Browse Source

修复07版整个excel仅存在数字时会出现的NPE

developing v2.0.4
Jiaju Zhuang 5 years ago
parent
commit
33e994e531
  1. 2
      pom.xml
  2. 2
      quickstart.md
  3. 9
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  4. 3
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  5. 4
      src/main/java/com/alibaba/excel/util/NumberUtils.java
  6. 2
      src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java
  7. 4
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.0.3</version> <version>2.0.4</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

2
quickstart.md

@ -7,9 +7,11 @@
* 单个文件的并发写入、读取 * 单个文件的并发写入、读取
* 读取图片 * 读取图片
* 宏 * 宏
ClassNotFoundException与java.lang.NoClassDefFoundError的区别
* csv读取(这个后续可能会考虑) * csv读取(这个后续可能会考虑)
#### 常见问题 #### 常见问题
* 关于@Data,读写的对象都用到了[Lombok](https://www.projectlombok.org/),他会自动生成`get`,`set` ,如果不需要的话,自己创建对象并生成`get`,`set` 。 * 关于@Data,读写的对象都用到了[Lombok](https://www.projectlombok.org/),他会自动生成`get`,`set` ,如果不需要的话,自己创建对象并生成`get`,`set` 。
* 出现`NoSuchMethodException`,`ClassNotFoundException`,`NoClassDefFoundError`极大概率是jar冲突,建议`clean`项目,或者统一`poi` 的版本,理论上来说`easyexcel`兼容poi的`3.17`,`4.0.1`,`4.1.0`所有较新版本
* 如果在读的时候`Listener`里面需要使用spring的`@Autowired`,给`Listener`创建成员变量,然后在构造方法里面传进去。而别必须不让spring管理`Listener`,每次读取都要`new`一个。 * 如果在读的时候`Listener`里面需要使用spring的`@Autowired`,给`Listener`创建成员变量,然后在构造方法里面传进去。而别必须不让spring管理`Listener`,每次读取都要`new`一个。
* 如果用`String`去接收数字,出现小数点等情况,这个是BUG,但是很难修复,后续版本会修复这个问题。目前请使用`@NumberFormat`直接,里面的参数就是调用了java自带的`NumberFormat.format`方法,不知道怎么入参的可以自己网上查询。 * 如果用`String`去接收数字,出现小数点等情况,这个是BUG,但是很难修复,后续版本会修复这个问题。目前请使用`@NumberFormat`直接,里面的参数就是调用了java自带的`NumberFormat.format`方法,不知道怎么入参的可以自己网上查询。
#### 详细参数介绍 #### 详细参数介绍

9
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -33,6 +33,7 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.FileUtils;
/** /**
@ -60,14 +61,18 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream); OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream);
readWorkbookHolder.setOpcPackage(pkg); readWorkbookHolder.setOpcPackage(pkg);
PackagePart sharedStringsTablePackagePart =
pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()).get(0); ArrayList<PackagePart> packageParts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
if (!CollectionUtils.isEmpty(packageParts)) {
PackagePart sharedStringsTablePackagePart = packageParts.get(0);
// Specify default cache // Specify default cache
defaultReadCache(readWorkbookHolder, sharedStringsTablePackagePart); defaultReadCache(readWorkbookHolder, sharedStringsTablePackagePart);
// Analysis sharedStringsTable.xml // Analysis sharedStringsTable.xml
analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), readWorkbookHolder); analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), readWorkbookHolder);
}
XSSFReader xssfReader = new XSSFReader(pkg); XSSFReader xssfReader = new XSSFReader(pkg);
analysisUse1904WindowDate(xssfReader, readWorkbookHolder); analysisUse1904WindowDate(xssfReader, readWorkbookHolder);

3
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java

@ -27,7 +27,6 @@ import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.util.StringUtils;
/** /**
* Cell Handler * Cell Handler
@ -106,6 +105,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void endHandle(String name) { public void endHandle(String name) {
currentTagDeque.pop();
// cell is formula // cell is formula
if (CELL_FORMULA_TAG.equals(name)) { if (CELL_FORMULA_TAG.equals(name)) {
currentCellData.setFormulaValue(formulaStringBuilder.toString()); currentCellData.setFormulaValue(formulaStringBuilder.toString());
@ -158,7 +158,6 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
currentCellData.checkEmpty(); currentCellData.checkEmpty();
curRowContent.put(curCol, currentCellData); curRowContent.put(curCol, currentCellData);
} }
currentTagDeque.pop();
} }
@Override @Override

4
src/main/java/com/alibaba/excel/util/NumberUtils.java

@ -26,8 +26,12 @@ public class NumberUtils {
public static String format(Number num, ExcelContentProperty contentProperty) { public static String format(Number num, ExcelContentProperty contentProperty) {
if (contentProperty == null || contentProperty.getNumberFormatProperty() == null if (contentProperty == null || contentProperty.getNumberFormatProperty() == null
|| StringUtils.isEmpty(contentProperty.getNumberFormatProperty().getFormat())) { || StringUtils.isEmpty(contentProperty.getNumberFormatProperty().getFormat())) {
if (num instanceof BigDecimal) {
return ((BigDecimal)num).toPlainString();
} else {
return num.toString(); return num.toString();
} }
}
String format = contentProperty.getNumberFormatProperty().getFormat(); String format = contentProperty.getNumberFormatProperty().getFormat();
RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode(); RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
DecimalFormat decimalFormat = new DecimalFormat(format); DecimalFormat decimalFormat = new DecimalFormat(format);

2
src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

@ -24,7 +24,7 @@ public class HgTest {
@Test @Test
public void hh() throws IOException { public void hh() throws IOException {
List<Object> list = List<Object> list =
EasyExcel.read(new FileInputStream("D:\\test\\折扣2007.xls")).headRowNumber(0).sheet().doReadSync(); EasyExcel.read(new FileInputStream("D:\\test\\1.xls")).headRowNumber(0).sheet().doReadSync();
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }

4
update.md

@ -1,3 +1,7 @@
# 2.0.4
* 修复07版整个excel仅存在数字时会出现的NPE
* 修复03版 用String接收电话会出现科学计数法的问题
# 2.0.3 # 2.0.3
* 修复重大bug 在07版读取文件的时候 小概率导致数字部分丢失 * 修复重大bug 在07版读取文件的时候 小概率导致数字部分丢失

Loading…
Cancel
Save