前言
結論先行。
開發過程中由于可能涉及到二次開發,若原系統開發時間久遠,沒有達成一致規范設計,導致風格各異,確實滿足當時開發場景,但增大了后續的更新的難度,容易出現俄羅斯套娃現象,新的更正引發新的問題和沖突,一環套一環。
如非必要,可延用老版本 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);