一次 POI 版本升级踩坑记录
前言
结论先行。
开发过程中由于可能涉及到二次开发,若原系统开发时间久远,没有达成一致规范设计,导致风格各异,确实满足当时开发场景,但增大了后续的更新的难度,容易出现俄罗斯套娃现象,新的更正引发新的问题和冲突,一环套一环。
如非必要,可延用老版本 poi 依赖。旧系统的迭代开发,更适合更新增加而非修改固有代码设计,牵一发而动全身。
一、版本升级触发条件
引入 hutool 新工具包,pom 依赖与原系统旧版本冲突,导致报错。故试图升级系统原有的poi版本。
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.6.4</version>
</dependency>
二、由于 poi 版本升级引起的其他错误,需更新部分设置适配新版本
1. poi 版本对比
旧版本 | 新版本 |
---|---|
3.13 | 5.2.2 |
<!-- 旧 -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.13</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.13</version>
</dependency><!-- 新 -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.2</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version>
</dependency>
2. Cell 类型对比
旧版本
switch (cell.getCellType()) {case Cell.CELL_TYPE_STRING:value = cell.getRichStringCellValue().getString().trim();break;case Cell.CELL_TYPE_NUMERIC:if (HSSFDateUtil.isCellDateFormatted(cell)){Date d = cell.getDateCellValue();value = formater.format(d);}else if ("General".equals(cell.getCellStyle().getDataFormatString())) {value = df.format(cell.getNumericCellValue());} else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {value = sdf.format(cell.getDateCellValue());} else {value = df2.format(cell.getNumericCellValue());}break;case Cell.CELL_TYPE_BOOLEAN:value = cell.getBooleanCellValue();break;case Cell.CELL_TYPE_BLANK:value = "";break;default:break;
}
新版本
switch (cell.getCellType()) {case STRING:value = cell.getRichStringCellValue().getString().trim();break;case NUMERIC:if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)){Date d = cell.getDateCellValue();value = formater.format(d);}else if ("General".equals(cell.getCellStyle().getDataFormatString())) {value = df.format(cell.getNumericCellValue());} else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {value = sdf.format(cell.getDateCellValue());} else {value = df2.format(cell.getNumericCellValue());}break;case BOOLEAN:value = cell.getBooleanCellValue();break;case BLANK:value = "";break;default:break;
}
3. 设置表头样式
旧版本
// 设置表头字体样式
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 列头的样式
HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderLeft((short) 1);
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderRight((short) 1);
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
新版本
// 设置表头字体样式
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBold(true);// 列头的样式
HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HorizontalAlignment.CENTER);
columnHeadStyle.setVerticalAlignment(VerticalAlignment.CENTER);
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(IndexedColors.BLACK.index);
columnHeadStyle.setBorderLeft(BorderStyle.THIN);
columnHeadStyle.setRightBorderColor(IndexedColors.BLACK.index);
columnHeadStyle.setBorderRight(BorderStyle.THIN);
columnHeadStyle.setBorderBottom(BorderStyle.THIN);
columnHeadStyle.setBottomBorderColor(IndexedColors.BLACK.index);
columnHeadStyle.setFillForegroundColor(IndexedColors.WHITE.index);