POI 3.6升级至3.17:XSSFCellStyle等常量替代方案咨询
POI 3.6 升级到 3.17:样式常量替代方案指南
针对你提到的三个POI样式常量,我整理了它们在3.17版本中的替代方案,结合你的代码片段给出具体修改建议:
1. XSSFCellStyle.SOLID_FOREGROUND 的替代
在POI 3.17中,填充模式常量被抽离到独立的枚举类 FillPatternType 中,替代方案是:
FillPatternType.SOLID_FOREGROUND
对应代码修改:
原代码:
style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
修改后:
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
需要导入包:org.apache.poi.ss.usermodel.FillPatternType
2. XSSFCellStyle.ALIGN_RIGHT 的替代
对齐方式常量也被重构为枚举类 HorizontalAlignment,替代方案是:
HorizontalAlignment.RIGHT
对应代码修改:
原代码:
cell.setCellStyle(ExcelCommon.getAligneCell(workbook, null, XSSFCellStyle.ALIGN_RIGHT));
修改后(需要同步调整getAligneCell方法的参数类型为HorizontalAlignment):
cell.setCellStyle(ExcelCommon.getAligneCell(workbook, null, HorizontalAlignment.RIGHT));
需要导入包:org.apache.poi.ss.usermodel.HorizontalAlignment
3. HSSFCellStyle.BORDER_MEDIUM 的替代
边框样式常量同样迁移到枚举类 BorderStyle 中,替代方案是:
BorderStyle.MEDIUM
对应代码修改:
原代码:
style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
修改后:
style.setBorderBottom(BorderStyle.MEDIUM);
需要导入包:org.apache.poi.ss.usermodel.BorderStyle
修改后的完整代码片段
// 加粗单元格样式部分 CellStyle fontBoldedCell = ExcelCommon.getFontBoldedCell(workbook); Sheet sheet = workbook.getSheetAt(0); row = sheet.createRow(currrow++); cell = row.createCell(0); cell.setCellValue("Total Records"); cell = row.createCell(1); cell.setCellValue(count); // 修改对齐方式常量 cell.setCellStyle(ExcelCommon.getAligneCell(workbook, null, HorizontalAlignment.RIGHT)); // 灰色背景样式部分 Font font = workbook.createFont(); font.setBold(true); CellStyle style = workbook.createCellStyle(); style.setFont(font); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); // 修改填充模式常量 style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setBorderTop(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN); return style; // 中等边框样式部分 CellStyle style = workbook.createCellStyle(); Font font = workbook.createFont(); style.setFont(font); // 修改边框样式常量 style.setBorderBottom(BorderStyle.MEDIUM);
内容的提问来源于stack exchange,提问作者Ishani Samaraweera




