Browse Source

* 修复某些特殊的excel读取失败的问题 [Issue #1595]

pull/2077/head
Jiaju Zhuang 3 years ago
parent
commit
5c7a4abd7e
  1. 6
      src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java
  2. 44
      src/main/java/com/alibaba/excel/util/StringUtils.java
  3. 2
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  4. 1
      update.md

6
src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java

@ -42,8 +42,14 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler
tempCellData.setType(CellDataTypeEnum.EMPTY);
break;
}
// 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");

44
src/main/java/com/alibaba/excel/util/StringUtils.java

@ -185,4 +185,48 @@ public class StringUtils {
return true;
}
/**
* <p>Checks if the CharSequence contains only Unicode digits.
* A decimal point is not a Unicode digit and returns false.</p>
*
* <p>{@code null} will return {@code false}.
* An empty CharSequence (length()=0) will return {@code false}.</p>
*
* <p>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.</p>
*
* <pre>
* 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
* </pre>
*
* @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;
}
}

2
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<Object> list = EasyExcel.read(file).sheet(0).doReadSync();
List<Object> list = EasyExcel.read("/Users/zhuangjiaju/Downloads/olay的副本.xlsx").sheet(0).doReadSync();
LOGGER.info("数据:{}", list.size());
for (Object data : list) {
LOGGER.info("返回数据:{}", CollectionUtils.size(data));

1
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

Loading…
Cancel
Save