poi导出Excel报错
项目启动后,第一次导出excel报Fail to save: an error occurs while saving the package : The part /docProps/core.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller@6da2de6;第二次导出居然可以,不知道大神有没有知道这个bug的
我的源码:
@ResponseBody
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> map) {
OutputStream outputStream = null;
try {
map.put("startDate", map.get("date"));
List<FirepreventDataEntity> list = firepreventRealdataService.list(map);
//date 导出日期
String date = ((String) map.get("date")).replace("-", "");
response.setContentType("application/binary;charset=ISO8859_1");
outputStream = response.getOutputStream();
String fileName = new String((date+Constant.FIREPREVENTREALEXCEL).getBytes(), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式
// 1.创建XSSFWorkbook,一个XSSFWorkbook对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 2.在workbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet(date);
// 3.设置表头,即每个列的列名
String[] titles = {"设备序号","设备编号","电箱状态","实时温度","CO浓度/PPM","烟雾探测","联动灭火器状态","记录时间"};
ExportUtil exportUtil = new ExportUtil(wb, sheet);
XSSFCellStyle titleStyle = exportUtil.getTitleStyle();
XSSFCellStyle mainStyle = exportUtil.getMainStyle();
CellRangeAddress cellRange = null;
XSSFRow titleRow = sheet.createRow(0);
titleRow.setHeightInPoints(18.75f);
XSSFCell tCell = titleRow.createCell(0);
tCell.setCellStyle(titleStyle);
tCell.setCellValue(date+Constant.FIREPREVENTREALEXCEL);
cellRange = new CellRangeAddress(0, 0, 0, 7);
sheet.addMergedRegion(cellRange);
// 构建表头
XSSFRow headRow = sheet.createRow(1);
XSSFCell cell = null;
for (int i = 0; i < titles.length; i++) {
cell = headRow.createCell(i);
cell.setCellStyle(mainStyle);
cell.setCellValue(titles[i]);
}
if(list != null && list.size() > 0) {
for(int i = 0; i < list.size(); i++) {
FirepreventDataEntity entity = list.get(i);
Integer outfire = entity.getOutfire();
XSSFRow bodyRow = sheet.createRow(i + 2);
cell = bodyRow.createCell(0);
cell.setCellStyle(mainStyle);
cell.setCellValue(entity.getNumber());
cell = bodyRow.createCell(1);
cell.setCellStyle(mainStyle);
cell.setCellValue(entity.getDeviceNo());
cell = bodyRow.createCell(2);
cell.setCellStyle(mainStyle);
cell.setCellValue(entity.getStatus());
cell = bodyRow.createCell(3);
cell.setCellStyle(mainStyle);
cell.setCellValue(entity.getTemperature());
cell = bodyRow.createCell(4);
cell.setCellStyle(mainStyle);
cell.setCellValue(entity.getCO());
cell = bodyRow.createCell(5);
cell.setCellStyle(mainStyle);
cell.setCellValue(entity.getSmoke()==0?"无烟":"有烟");
cell = bodyRow.createCell(6);
cell.setCellStyle(mainStyle);
cell.setCellValue(outfire==0?"正常":outfire==1?"热敏线启动":outfire==2?"电启动":"电启动故障");
cell = bodyRow.createCell(7);
cell.setCellStyle(mainStyle);
cell.setCellValue(DateUtils.yyyy_MM_dd(entity.getRecordTime()));
}
}
sheet.setColumnWidth(7, 20 * 256);
wb.write(outputStream);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
logger.error("firepreventRealdataController导出错误 error ==> {}, 参数 ==> {}",e.getMessage(),map);
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}