專案啟動后,第一次匯出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();
}
}
}
uj5u.com熱心網友回復:
通過debug除錯,是在wb.write(outputStream);這句報錯,哪位大神知道怎么回事啊uj5u.com熱心網友回復:
我剛分享了個poi匯出Excel的,可以看看
希望幫得到你https://blog.csdn.net/Gemini_Kanon/article/details/90762894
uj5u.com熱心網友回復:
看不大出來ε=(′ο`*)))~uj5u.com熱心網友回復:
POI會自己創建臨時檔案夾/tmp/poifiles,會不會是你的應用沒有權限?uj5u.com熱心網友回復:
找到原因啦,是springboot的tomcat連接超時時間過短,造成http連接超時,把server.connection-timeout的時間調長就行了
uj5u.com熱心網友回復:
優化匯出介面時間,我也遇到了,window上google匯出7000條可以,蘋果電腦匯出就出問題,蘋果電腦匯出幾百條也沒問題,感謝樓主uj5u.com熱心網友回復:
匯入 import cn.hutool.core.*@GetMapping("/export")
@ApiOperation(value = "匯出",httpMethod = "GET")
public void test3( FeedbackListParam param, HttpServletResponse response) {
ExcelWriter writer = ExcelUtil.getWriter();
try {
/**
*/
String head = "匯出資料";
List<List<Object>> rows = new LinkedList<>();
//填充rows
List<String> rowHead = CollUtil.newArrayList("id", "用戶id", "用戶昵稱");
writer.writeHeadRow(rowHead);
writer.write(rows);
//設定寬度自適應
writer.setColumnWidth(-1, 22);
//response為HttpServletResponse物件
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//test.xls是彈出下載對話框的檔案名,不能為中文,中文請自行編碼
response.setHeader("Content-Disposition", "attachment;filename=" + new String((head).getBytes("UTF-8"), "ISO-8859-1") + ".xls");
ServletOutputStream out = response.getOutputStream();
//out為OutputStream,需要寫出到的目標流
writer.flush(out);
} catch (Exception e) {
log.error("匯出例外",e);
e.printStackTrace();
} finally {
// 關閉writer,釋放記憶體
writer.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/252853.html
標籤:Web 開發
上一篇:一個演算法方法 不太看的懂
