前言
描述:项目中的 poi 包有点旧,很多方法都没有,现在导入新的 poi 包,导致原先项目中的 代码报错。
描述:下面是遇到的一些报错,解决方案。
报错1:CellStyle.BORDER_THIN
代码如下:
// cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左边边框
cellStyle.setBorderLeft(BorderStyle.THIN); // 左边边框
解决办法:将 BorderStyle.THIN
替换 注释中的代码即可
报错2:CellStyle.ALIGN_CENTER
代码如下:
// cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格水平方向对其方式
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 设置单元格水平方向对其方式
解决办法:将 HorizontalAlignment.CENTER
替换 旧的即可
所在包:import org.apache.poi.ss.usermodel.HorizontalAlignment;
报错3:XSSFCellStyle.VERTICAL_CENTER
代码如下:
// cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//垂直
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直
解决办法:将 VerticalAlignment.CENTER
替换 旧的即可
所在包:import org.apache.poi.ss.usermodel.VerticalAlignment;
报错4:Cell.CELL_TYPE_BLANK
代码如下:
//Cell.CELL_TYPE_BLANK
CellType.BLANK
解决办法:将 CellType.BLANK
替换 旧的即可
所在包:import org.apache.poi.ss.usermodel.CellType;
报错5:redFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//粗体显示
代码如下:
// redFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//粗体显示
redFont.setBold(true);
解决办法:使用 setBold() 方法即可
报错6:CellStyle.SOLID_FOREGROUND
代码如下:
// cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
解决办法:FillPatternType.SOLID_FOREGROUND
所在包:import org.apache.poi.ss.usermodel.FillPatternType;
报错7:switch. case. (HSSFCell.CELL_TYPE_STRING)
旧代码示例:
int cType = fromCell.getCellType();
switch (cType) {
case HSSFCell.CELL_TYPE_STRING:
break;
case HSSFCell.CELL_TYPE_NUMERIC:
break;
case HSSFCell.CELL_TYPE_FORMULA:
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
break;
case HSSFCell.CELL_TYPE_ERROR:
break;
default:
break;
}
改为新代码:
CellType cType = fromCell.getCellType();
switch (cType) {
case STRING:
break;
case NUMERIC:
break;
case FORMULA:
break;
case BOOLEAN:
break;
case ERROR:
break;
default:
break;
}
报错8:anchor.setAnchorType(3);
报错代码:
HSSFClientAnchor anchor = new HSSFClientAnchor(27,67,987,194,(short)col11,row1,(short) col22,row2);
anchor.setAnchorType(3);
修改为: anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
所在包:import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
报错9:Region
报错代码:
Region region = null;
region = fromsheet.getMergedRegionAt(0);
if ((region.getRowFrom() >= firstrow)&& (region.getRowTo() <= lastrow)) {
region.setRowFrom(region.getRowFrom()+1);
region.setRowTo(region.getRowTo()+1);
region.setColumnFrom((short) (region.getColumnFrom()+6));
region.setColumnTo((short) (region.getColumnTo()+6));
}
解决办法:
CellRangeAddress region = null;
region = fromsheet.getMergedRegion(0);
if ((region.getFirstRow() >= firstrow)&& (region.getLastRow() <= lastrow)) {
region.setFirstRow(region.getFirstRow()+1);
region.setLastRow(region.getLastRow()+1);
region.setFirstColumn((short) (region.getFirstColumn()+6));
region.setLastColumn((short) (region.getLastColumn()+6));
}
所在包名:import org.apache.poi.ss.util.CellRangeAddress;
报错10 new XSSFWorkbook();
报错内容:
[java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream]
内容分析:缺少这个UnsynchronizedByteArrayOutputStream.class
解决办法:升级jar 包
导入依赖如下:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
如果改了之后,还不行,又报错了
报错如下所示:
[java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap]
解决办法:升级jar 包
导入依赖如下:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
改了还是报错:
报错如下:
[java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager]
导入依赖:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
还是报错,
报错如下: [java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile]
导入依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</dependency>
报错如下: [java.lang.NoClassDefFoundError: org/apache/xmlbeans/impl/schema/DocumentFactory]
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.0.2</version>
</dependency>