public ResponseEntity<byte[]> exportEmp() {
return PoiUtils.exportEmp2Excel(empService.getAllEmployees());
}
public class PoiUtils {
public static ResponseEntity<byte[]> exportEmp2Excel(List<Employee> emps) {
ByteArrayOutputStream baos = null;
try {
//生成Excel,出于篇幅考虑,这里省略掉,小伙伴可以直接在源码中查看
headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", new String("员工表.xls".getBytes("UTF-8"), "iso-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
workbook.write(baos);
} catch (IOException e) {
e.printStackTrace();
}
return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
}