org.apache.poi版本不兼容解决

在导入文件的时候会使用到org.apache.poi的依赖,版本为:

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>4.1.0</version>
</dependency>

但是呢,在使用导出的时候会出现报错,网上多数的excel导出工具类设置单元格居中的样式都是hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);但是在4.1的poi包里没有这个方法,就要进行转换成cellStyle.setAlignment(HorizontalAlignment.CENTER);这种因为poi包从3.7以后到4.1出现了不兼容情况,需要更改写法,可以参考下面

3.6写法:

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

4.1写法:

cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());

 

 

PoI 3.17 已过时代码对比
颜色定义变化
旧版本 : HSSFColor.BLACK.index
新版本 : IndexedColors.BLACK.index

获取单元格格式
旧版本 : cell.getCellType 与之应对的单元格格式 HSSFCell.CELL_TYPE_BLANK
新版本 : cell.getCellTypeEnum 与之对应的单元格格式 BLANK (org.apache.poi.ss.usermodel.CellType.BLANK)

设置单元格格式
旧版本 : row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
新版本 : row.getCell(0).setCellType(CellType.STRING);

设置单元格垂直居中样式
旧版本 : XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
    cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//垂直
新版本 : XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直

设置边框
旧版本 : cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
新版本 : cellStyle.setBorderBottom(BorderStyle.THIN); //下边框

合并单元格
旧版本 : sheet.addMergedRegion(new CellRangeAddress(1, 1,(short) 0, (short) 0));// 起始行,结束行,起始列,结束列
新版本 : sheet.addMergedRegion(new Region(1, (short) 0, 1,(short) 0));// 起始行,起始列,结束行,结束列

设置字体加粗
旧版本: font.setBoldweight((short) 400);
新版本: font.setBold(true);

posted on   &大飞  阅读(3457)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示