<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
@ApiOperation(“SXSSFWorkbook 百万数据导出”)
@RequestMapping( value = “/SXSSFWorkbook”,method = RequestMethod.GET)
public MelotResult exportMore(HttpServletResponse response) throws IOException {
//2.创建工作簿
SXSSFWorkbook workbook = new SXSSFWorkbook();
//3.构造sheet
String[] titles = {“编号”, “姓名”, “手机”,“高学历”, “国家地区”, “护照号”,
“籍贯”, “生日”, “属相”,“入职时间”,“离职类型”,“离职原因”,“离职时间”};
Sheet sheet = workbook.createSheet(“sheet1”);
Row row = sheet.createRow(0);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
for (int i =0;i<13;i++){
sheet.setColumnWidth(i, 5000);//设置列的宽度是个字符宽度
}
AtomicInteger headersAi = new AtomicInteger();
for (String title : titles) {
Cell cell = row.createCell(headersAi.getAndIncrement());
cell.setCellStyle(cellStyle);
cell.setCellValue(title);
}
AtomicInteger datasAi = new AtomicInteger(1);
Cell cell = null;
for(int i =0;i<100;i++){
for (EmployeeReportResult report : EmployeeReportResult.getBeanList()) {
Row dataRow = sheet.createRow(datasAi.getAndIncrement());
//编号
cell = dataRow.createCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getUserId());
//姓名
cell = dataRow.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getUsername());
//手机
cell = dataRow.createCell(2);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getMobile());
//高学历
cell = dataRow.createCell(3);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getTheHighestDegreeOfEducation());
//国家地区
cell = dataRow.createCell(4);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getNationalArea());
//护照号
cell = dataRow.createCell(5);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getPassportNo());
//籍贯
cell = dataRow.createCell(6);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getNativePlace());
//生日
cell = dataRow.createCell(7);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getBirthday());
//属相
cell = dataRow.createCell(8);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getZodiac());
//入职时间
cell = dataRow.createCell(9);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getTimeOfEntry());
//离职类型
cell = dataRow.createCell(10);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getTypeOfTurnover());
//离职原因
cell = dataRow.createCell(11);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getReasonsForLeaving());
//离职时间
cell = dataRow.createCell(12);
cell.setCellStyle(cellStyle);
cell.setCellValue(report.getResignationTime());
}
}
/* //6.文件流
FileOutputStream fos = new FileOutputStream("E:\\test.xlsx");
//7.写入文件
workbook.write(fos);
fos.close();*/
String fileName = URLEncoder.encode("人员信息.xlsx", "UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1")));
workbook.write(response.getOutputStream());
return new MelotResult<>("");
}