模板导出Excel
filemanagement - ExcelExportOfTemplateUtil.java L633
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17String 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<String, Object> map = new HashMap<String, Object>(); BlApply blApply = new BlApply(); blApply.setShipper("shipper"); blApply.setShipperAddress("shipperAddress"); // 原生 easypoi 不支持"n"或者"rn"换行符 blApply.setShipper(blApply.getShipper() + "n" + blApply.getShipperAddress()); Map<String, Object> 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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19// 源码内容 /** * 给每个Cell通过解析方式set值 * * @param cell * @param map */ private void setValueForCellByMap(Cell cell, Map<String, Object> map) 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); } }
- 重写方法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21private void setValueForCellByMap(Cell cell, Map<String, Object> map, 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); } } }
最后
以上就是幽默网络最近收集整理的关于easypoi不支持换行符的全部内容,更多相关easypoi不支持换行符内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复