Skip to content

Commit

Permalink
* 修复某些特殊的excel读取失败的问题 [Issue #1595]
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Sep 15, 2021
1 parent 51f2227 commit 5c7a4ab
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ public void endElement(XlsxReadContext xlsxReadContext, String name) {
tempCellData.setStringValue(tempData.toString());
break;
case BOOLEAN:
if(StringUtils.isEmpty(tempDataString)){
if (StringUtils.isEmpty(tempDataString)) {
tempCellData.setType(CellDataTypeEnum.EMPTY);
break;
}
tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString()));
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");
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/alibaba/excel/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,48 @@ public static boolean regionMatches(final CharSequence cs, final boolean ignoreC
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c7a4ab

Please sign in to comment.