From 5c7a4abd7e1135680ce1be4aa102bb0f41fb9fc0 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 15 Sep 2021 23:50:50 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E4=BF=AE=E5=A4=8D=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E7=9A=84excel=E8=AF=BB=E5=8F=96=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=20[Issue=20#1595]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handlers/AbstractCellValueTagHandler.java | 14 ++++-- .../com/alibaba/excel/util/StringUtils.java | 44 +++++++++++++++++++ .../easyexcel/test/temp/Lock2Test.java | 2 +- update.md | 1 + 4 files changed, 56 insertions(+), 5 deletions(-) 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 5cc75cdc..bf8dfecc 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 @@ -30,7 +30,7 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler tempCellData.setStringValue(tempData.toString()); break; case BOOLEAN: - if(StringUtils.isEmpty(tempDataString)){ + if (StringUtils.isEmpty(tempDataString)) { tempCellData.setType(CellDataTypeEnum.EMPTY); break; } @@ -38,12 +38,18 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler break; case NUMBER: case EMPTY: - if(StringUtils.isEmpty(tempDataString)){ + if (StringUtils.isEmpty(tempDataString)) { tempCellData.setType(CellDataTypeEnum.EMPTY); break; } - tempCellData.setType(CellDataTypeEnum.NUMBER); - tempCellData.setNumberValue(BigDecimal.valueOf(Double.parseDouble(tempDataString))); + // fix https://github.com/alibaba/easyexcel/issues/1595 + if (StringUtils.isNumeric(tempDataString)) { + tempCellData.setType(CellDataTypeEnum.NUMBER); + tempCellData.setNumberValue(BigDecimal.valueOf(Double.parseDouble(tempDataString))); + } else { + tempCellData.setType(CellDataTypeEnum.STRING); + tempCellData.setStringValue(tempData.toString()); + } break; default: throw new IllegalStateException("Cannot set values now"); diff --git a/src/main/java/com/alibaba/excel/util/StringUtils.java b/src/main/java/com/alibaba/excel/util/StringUtils.java index cb6e7227..6c636908 100644 --- a/src/main/java/com/alibaba/excel/util/StringUtils.java +++ b/src/main/java/com/alibaba/excel/util/StringUtils.java @@ -185,4 +185,48 @@ public class StringUtils { return true; } + + /** + *

Checks if the CharSequence contains only Unicode digits. + * A decimal point is not a Unicode digit and returns false.

+ * + *

{@code null} will return {@code false}. + * An empty CharSequence (length()=0) will return {@code false}.

+ * + *

Note that the method does not allow for a leading sign, either positive or negative. + * Also, if a String passes the numeric test, it may still generate a NumberFormatException + * when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range + * for int or long respectively.

+ * + *
+     * StringUtils.isNumeric(null)   = false
+     * StringUtils.isNumeric("")     = false
+     * StringUtils.isNumeric("  ")   = false
+     * StringUtils.isNumeric("123")  = true
+     * StringUtils.isNumeric("\u0967\u0968\u0969")  = true
+     * StringUtils.isNumeric("12 3") = false
+     * StringUtils.isNumeric("ab2c") = false
+     * StringUtils.isNumeric("12-3") = false
+     * StringUtils.isNumeric("12.3") = false
+     * StringUtils.isNumeric("-123") = false
+     * StringUtils.isNumeric("+123") = false
+     * 
+ * + * @param cs the CharSequence to check, may be null + * @return {@code true} if only contains digits, and is non-null + * @since 3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence) + * @since 3.0 Changed "" to return false and not true + */ + public static boolean isNumeric(final CharSequence cs) { + if (isEmpty(cs)) { + return false; + } + final int sz = cs.length(); + for (int i = 0; i < sz; i++) { + if (!Character.isDigit(cs.charAt(i))) { + return false; + } + } + return true; + } } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 94f8ca31..cc81f194 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -36,7 +36,7 @@ public class Lock2Test { public void test() throws Exception { File file = TestFileUtil.readUserHomeFile("test/test4.xlsx"); - List list = EasyExcel.read(file).sheet(0).doReadSync(); + List list = EasyExcel.read("/Users/zhuangjiaju/Downloads/olay的副本.xlsx").sheet(0).doReadSync(); LOGGER.info("数据:{}", list.size()); for (Object data : list) { LOGGER.info("返回数据:{}", CollectionUtils.size(data)); diff --git a/update.md b/update.md index 4167a00a..8e976243 100644 --- a/update.md +++ b/update.md @@ -22,6 +22,7 @@ * 修复填充调用横向样式策略报错 [Issue #1651](https://github.com/alibaba/easyexcel/issues/1651) * 修复不自动行高的问题 [Issue #1869](https://github.com/alibaba/easyexcel/issues/1869) * 新增头的非空校验 [Issue #1765](https://github.com/alibaba/easyexcel/issues/1765) +* 修复某些特殊的excel读取失败的问题 [Issue #1595](https://github.com/alibaba/easyexcel/issues/1595) # 2.2.10