FastExcel替换EasyExcel解析表格报错
yangweibing opened this issue · 5 comments
问题一:
解析的表格带有批注:
原来用esayExcel是可以解析出标头中的批注,代码如下:
public static void readExcelFirstRow() {
List<String> resultList = new LinkedList<>();
Map<String, String> commentMap = new HashMap<>();
List<List<String>> cacheList = new LinkedList<>();
try {
File file = new File("C:\\Users\\ywb\\Downloads\\test.xlsx");
InputStream inp = new FileInputStream(file);
ExcelReaderBuilder registerReadListener = EasyExcel.read(inp)
.extraRead(CellExtraTypeEnum.COMMENT) // 开启读取批注
.head(cacheList).headRowNumber(0)
.registerReadListener(new PageReadListener<Map<Integer, String>>(dataList -> {
// 默认只读1页
int row = 0;
for (Map<Integer, String> data : dataList) {
if (resultList.isEmpty()) {
for (Map.Entry<Integer, String> entry : data.entrySet()) {
int column = entry.getKey();
String value = entry.getValue();
String comment = commentMap.get(row + "#" + column);
if (comment != null) {
resultList.add(value + "#" + comment);
}
else {
resultList.add(value);
}
}
}
else {
// 读取一行后抛异常停止读取
throw new ExcelAnalysisStopException();
}
row++;
}
},100) {
@Override
public void extra(CellExtra extra, AnalysisContext context) {
// EasyExcel先读取表格的额外内容再读取正文,因此需要先暂存额外内容
if (extra.getType() == CellExtraTypeEnum.COMMENT) {
commentMap.put(extra.getRowIndex() + "#" + extra.getColumnIndex(), extra.getText());
}
}
});
registerReadListener.sheet().doRead();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
把上面的代码中的EasyExcel换成fastExcel就不行了,对应的所有引用也都替换了。以下是错误输出:
java.lang.NoSuchMethodError: org.apache.logging.log4j.Logger.atDebug()Lorg/apache/logging/log4j/LogBuilder;
maven引入如下:
<dependency>
<groupId>cn.idev.excel</groupId>
<artifactId>fastexcel</artifactId>
<version>1.0.0</version> <!-- 请确保使用最新版本 -->
</dependency>
问题二:esayExcel的registerReadListener目前只支持数据数量小于100条的表格解析,大于100条,就不走extra方法,无法读取批注,请问fastExcel有什么好的解决办法?目前只能用的Workbook来实现。
No.1 :The issue appears to be a Log4j JAR version conflict. Try checking Maven and excluding the conflicting version.
我看了源码里面的slf4j-api 版本是1.7.36。我把pom.xml的slf4j-api版本也改成1.7.36,仍然报错:
java.lang.NoSuchMethodError: org.apache.logging.log4j.Logger.atDebug()Lorg/apache/logging/log4j/LogBuilder;
pom.xml添加如下引用,问题1解决:
org.apache.logging.log4j
log4j-to-slf4j
2.17.2
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
原因是spring-boot-starter 版本的原因导致log4j缺失atDebug()方法。
执行成功后,FastExcel的结果和EasyExcel效果一样,小于100行可以解析出批注,大于100行不执行extra
You’ve set batchCount to 100; why not consider using a straightforward ReadListener?
你指定了batchCount为100,为什么不尝试使用最简单的ReadListener
呢?
使用ReadListener的extra和invokeHead组合确实可以解析出标头和批注,感谢!