在 Apache POI 的较新版本中,许多旧的 API 已被弃用或移除,取而代之的是更现代、更规范的 API。以下是关于 POI 升级后需要进行的常见替换的总结:
1. 替换 HSSFCellStyle 和 XSSFCellStyle
旧代码:
> HSSFCellStyle style = workbook.createCellStyle();
> style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
> style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
> style.setBorderBottom((short) 1);
> style.setFillForegroundColor(HSSFColor.RED.index);
> style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
新代码:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 居中对齐
style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中对齐
style.setBorderBottom(BorderStyle.THIN); // 细边框
style.setFillForegroundColor(IndexedColors.RED.getIndex()); // 红色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 纯色填充
2. 替换 HSSFFont 和 XSSFFont
旧代码:
HSSFFont font = workbook.createFont();
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 14);
新代码:
import org.apache.poi.ss.usermodel.*;
Font font = workbook.createFont();
font.setFontName("微软雅黑");
font.setBold(true); // 加粗
font.setFontHeightInPoints((short) 14); // 字体大小
3. 替换 HSSFColor
旧代码:
style.setFillForegroundColor(HSSFColor.RED.index);
新代码:
import org.apache.poi.ss.usermodel.IndexedColors;
style.setFillForegroundColor(IndexedColors.RED.getIndex());
4. 替换 Cell.CELL_TYPE_*
旧代码:
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
// 处理数值
}
新代码:
import org.apache.poi.ss.usermodel.CellType;
if (cell.getCellType() == CellType.NUMERIC) {
// 处理数值
}
5. 替换 Region
旧代码:
Region region = new Region(0, (short) 0, 1, (short) 1);
sheet.addMergedRegion(region);
新代码:
import org.apache.poi.ss.util.CellRangeAddress;
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, 0, 1);
sheet.addMergedRegion(cellRangeAddress);
6. 替换 ClientAnchor.MOVE_AND_RESIZE
旧代码:
anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
新代码:
import org.apache.poi.ss.usermodel.ClientAnchor;
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
7. 替换 XSSFColor
旧代码:
XSSFColor color = new XSSFColor(new Color(0xdf, 0x56, 0x40));
新代码:
import org.apache.poi.xssf.usermodel.XSSFColor;
import java.awt.Color;
XSSFColor color = new XSSFColor(new Color(0xdf, 0x56, 0x40), null);
8. 替换 HSSFDateUtil
旧代码:
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 处理日期
}
新代码:
import org.apache.poi.ss.usermodel.DateUtil;
if (DateUtil.isCellDateFormatted(cell)) {
// 处理日期
}
9. 替换 POIXMLDocument
旧代码:
import org.apache.poi.POIXMLDocument;
POIXMLDocument document = POIXMLDocument.open("example.xlsx");
新代码:
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
try (FileInputStream file = new FileInputStream("example.xlsx")) {
XSSFWorkbook workbook = new XSSFWorkbook(file);
// 处理工作簿
} catch (IOException e) {
e.printStackTrace();
}
10. 替换 CTP.Factory.newInstance()
旧代码:
CTP ctp = CTP.Factory.newInstance();
XWPFParagraph paragraph = new XWPFParagraph(ctp, null);
新代码:
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
总结
使用 CellType 枚举替代 Cell.CELL_TYPE_*。
使用 HorizontalAlignment 和 VerticalAlignment 枚举替代 short 类型的对齐参数。
使用 IndexedColors 替代 HSSFColor。
使用 CellRangeAddress 替代 Region。
使用 XSSFColor.from(Color) 替代 new XSSFColor(Color)。
使用 DateUtil 替代 HSSFDateUtil。
使用 XWPFDocument 和 XWPFParagraph 替代 CTP.Factory.newInstance()。
确保使用较新的 POI 版本(如 5.x 或更高版本),并更新 Maven 或 Gradle 依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>