From cebc52175a5b0ad4eb4098bbef4fdfb7e0e3165e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=AF=E4=BB=AA?= Date: Tue, 16 Mar 2021 21:54:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B907=E5=9C=A8=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E7=9A=84excel=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../v07/handlers/AbstractCellValueTagHandler.java | 10 ++++++++++ .../analysis/v07/handlers/CellValueTagHandler.java | 5 +++++ .../java/com/alibaba/easyexcel/test/temp/LockTest.java | 6 +----- update.md | 3 +++ 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 641dd476..6c90beb4 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.2.7 + 2.2.8 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java index 3521e46a..f10fa5a4 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java @@ -7,6 +7,7 @@ import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.util.BooleanUtils; +import com.alibaba.excel.util.StringUtils; /** * Cell Value Handler @@ -20,6 +21,7 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); CellData tempCellData = xlsxReadSheetHolder.getTempCellData(); StringBuilder tempData = xlsxReadSheetHolder.getTempData(); + String tempDataString = tempData.toString(); CellDataTypeEnum oldType = tempCellData.getType(); switch (oldType) { case DIRECT_STRING: @@ -28,10 +30,18 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler tempCellData.setStringValue(tempData.toString()); break; case BOOLEAN: + if(StringUtils.isEmpty(tempDataString)){ + tempCellData.setType(CellDataTypeEnum.EMPTY); + break; + } tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString())); break; case NUMBER: case EMPTY: + if(StringUtils.isEmpty(tempDataString)){ + tempCellData.setType(CellDataTypeEnum.EMPTY); + break; + } tempCellData.setType(CellDataTypeEnum.NUMBER); tempCellData.setNumberValue(new BigDecimal(tempData.toString())); break; diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java index 09c9264e..7e685c97 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java @@ -3,6 +3,7 @@ package com.alibaba.excel.analysis.v07.handlers; import com.alibaba.excel.context.xlsx.XlsxReadContext; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.util.StringUtils; /** * Cell Value Handler @@ -17,6 +18,10 @@ public class CellValueTagHandler extends AbstractCellValueTagHandler { CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData(); switch (tempCellData.getType()) { case STRING: + // In some cases, although cell type is a string, it may be an empty tag + if(StringUtils.isEmpty(tempCellData.getStringValue())){ + break; + } String stringValue = xlsxReadContext.readWorkbookHolder().getReadCache() .get(Integer.valueOf(tempCellData.getStringValue())); if (stringValue != null && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java index 27f820bc..ad09717f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java @@ -26,11 +26,7 @@ public class LockTest { public void test() throws Exception { List list = - EasyExcel.read(new FileInputStream("D:\\test\\testbug嘉惠.xlsx")).sheet().headRowNumber(0).doReadSync(); - for (Object data : list) { - LOGGER.info("返回数据:{}", JSON.toJSONString(data)); - } - list = EasyExcel.read(new File("D:\\test\\t222.xlsx")).sheet().headRowNumber(0).doReadSync(); + EasyExcel.read(new FileInputStream("/Users/zhuangjiaju/Downloads/点位配置表 (1).xlsx")).doReadAllSync(); for (Object data : list) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } diff --git a/update.md b/update.md index d7a96f7c..9773c955 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.2.8 +* 兼容07在特殊的excel的情况下,读取数据异常 + # 2.2.7 * 修改07在特殊情况下用`String`接收数字会丢小数位的bug