poi导出Excel报错

qq_40966182 2019-06-04 04:56:10
项目启动后,第一次导出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();
}
}
}
...全文
7242 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yorky 2022-03-31
  • 打赏
  • 举报
回复

本人亲测,此问题解决方案,终极方法,更换

org.jxls
jxls-poi
${jxlspoi.version}

版本即可解决,更换一个下载次数比较多的版本,完美解决。

qq_42859257 2021-01-26
  • 打赏
  • 举报
回复
优化导出接口时间,我也遇到了,window上google导出7000条可以,苹果电脑导出就出问题,苹果电脑导出几百条也没问题,感谢楼主
lin351550660 2021-01-26
  • 打赏
  • 举报
回复
导入 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();
		}
	}
qq_40966182 2019-06-17
  • 打赏
  • 举报
回复 6
找到原因啦,是springboot的tomcat连接超时时间过短,造成http连接超时,把server.connection-timeout的时间调长就行了
对梦想的牵挂 2019-06-10
  • 打赏
  • 举报
回复
POI会自己创建临时文件夹/tmp/poifiles,会不会是你的应用没有权限?
Sunyiban 2019-06-06
  • 打赏
  • 举报
回复
看不大出来ε=(´ο`*)))~
Gemini_Kanon 2019-06-05
  • 打赏
  • 举报
回复
我刚分享了个poi导出Excel的,可以看看希望帮得到你https://blog.csdn.net/Gemini_Kanon/article/details/90762894
qq_40966182 2019-06-04
  • 打赏
  • 举报
回复 8
通过debug调试,是在wb.write(outputStream);这句报错,哪位大神知道怎么回事啊

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧