导出部分代码:
try {
resp.setHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls"); //设置文件头编码格式
resp.setContentType("application/vnd.ms-excel");//设置类型
resp.setHeader("Cache-Control", "no-cache");//设置头
resp.setDateHeader("Expires", 0);//设置日期头
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet(sheetName);
CellStyle cellStyle = book.createCellStyle();
cellStyle.setDataFormat(book.createDataFormat().getFormat("yyyy-MM-dd"));
// 设置列名
Row row = sheet.createRow(0);
for (int i = 0; i < columnNames.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(columnNames[i]);
}
//设置表格内容
int rowCount = 1;
for (int i = 0; i < exportList.size(); i++) {
EmployeeExcelData data = exportList.get(i);
row = sheet.createRow(rowCount);
rowCount++;
Cell cell1 = row.createCell(0);
cell1.setCellValue(data.getDeptId());
Cell cell2 = row.createCell(1);
cell2.setCellValue(data.getDeptName());
Cell cell3 = row.createCell(2);
cell3.setCellValue(data.getActualName());
Cell cell4 = row.createCell(3);
cell4.setCellValue(data.getUsername());
Cell cell5 = row.createCell(4);
cell5.setCellValue(data.getPhone());
Cell cell6 = row.createCell(5);
cell6.setCellValue(data.getRankIndex());
Cell cell7 = row.createCell(6);
cell7.setCellValue(data.getRuleName());
Cell cell8 = row.createCell(7);
cell8.setCellValue(data.getMail());
}
book.write(resp.getOutputStream());
resp.getOutputStream().flush();
resp.getOutputStream().close();
} catch (Exception e) {
resp.reset();
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
System.out.println("导出失败");
}
}
office文件所对应的的 Content-type类型总结:
https://blog.csdn.net/gp_911014/article/details/108103428