filemanagement - ExcelExportOfTemplateUtil.java L633
String filePath = rootPath + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + filename;
TemplateExportParams params = new TemplateExportParams(filePath, true);
File tempFile = new File(filePath);
if (!tempFile.exists()) {
tempFile.createNewFile();
}
TemplateExportParams params = new TemplateExportParams(filePath, true);
Map map = new HashMap();
BlApply blApply = new BlApply();
blApply.setShipper("shipper");
blApply.setShipperAddress("shipperAddress");
// 原生 easypoi 不支持"n"或者"rn"换行符
blApply.setShipper(blApply.getShipper() + "n" + blApply.getShipperAddress());
Map obj = JSON.parseObject(JSONObject.toJSONString(blApply), Map.class);
map.put("obj", obj);
ExcelExportUtil.exportExcel(params, map);
疑似异常
- 遇到问题已经过去一个月,当时的异常好像是 java.lang.ClassCastException ****
- 也可能没有异常,只是没有换行而已
- easypoi版本 4.4.0
- 尝试升级版本,也不支持环境
- ExcelExportOfTemplateUtil.setValueForCellByMap
// 源码内容 private void setValueForCellByMap(Cell cell, Mapmap) throws Exception { ... if (obj instanceof ImageEntity) { ... } else if (isNumber && StringUtils.isNotBlank(obj.toString())) { cell.setCellValue(Double.parseDouble(obj.toString())); } else { // TODO 2022-06-13 重写这部分即可(不支持换行符) cell.setCellValue(valueStr); } }
- 重写方法
private void setValueForCellByMap(Cell cell, Mapmap, Workbook workbook) throws Exception { ... if (obj instanceof ImageEntity) { ... } else if (isNumber && StringUtils.isNotBlank(obj.toString())) { cell.setCellValue(Double.parseDouble(obj.toString())); } else { String valueStr = obj.toString(); if (StringUtils.isNotBlank(valueStr) && valueStr.indexOf("n") >= 0) { // 修改源代码(支持回车换行) cell.setCellValue(new XSSFRichTextString(valueStr)); CellStyle cellStyle = workbook.createCellStyle(); // 也是支持回车换行符(可能不用设置这部分也可以) cellStyle.setWrapText(true); cell.setCellStyle(cellStyle); } else { cell.setCellValue(valueStr); } } }



