@GetMapping("/downExcelFile")
public void test(HttpServletResponse response) {
OutputStream outputStream = null;
try {
response.addHeader("Content-Disposition", "attachment;filename=" + "test.xlsx");
response.setContentType("application/vnd.ms-excel");
Workbook workbook = new SXSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
DataFormat dataFormat = workbook.createDataFormat();
short format = dataFormat.getFormat("@");
cellStyle.setDataFormat(format);
Sheet sheet = workbook.createSheet("计划模板");
for (int i = 0; i < 1000; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellStyle(cellStyle);
cell.setCellValue(i + "" + j);
}
}
sheet.setDefaultColumnStyle(0, cellStyle);
outputStream = new BufferedOutputStream(response.getOutputStream());
workbook.write(outputStream);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}