Skip to content

Commit 937f620

Browse files
authored
bugfix: Fix Potential Data Read Failures Caused by Custom Data Formats.by @zyd0131 at #2319 (#156)
1 parent 2e10c3b commit 937f620

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

fastexcel-core/src/main/java/cn/idev/excel/metadata/format/DataFormatter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public class DataFormatter {
118118
*/
119119
private static final String invalidDateTimeString;
120120

121+
private static final BigDecimal TEN = new BigDecimal(10);
122+
121123
static {
122124
StringBuilder buf = new StringBuilder();
123125
for (int i = 0; i < 255; i++) {buf.append('#');}
@@ -524,11 +526,17 @@ public InternalDecimalFormatWithScale(String pattern, DecimalFormatSymbols symbo
524526
setExcelStyleRoundingMode(df);
525527
Matcher endsWithCommasMatcher = endsWithCommas.matcher(pattern);
526528
if (endsWithCommasMatcher.find()) {
529+
int index_point = pattern.indexOf(".");
530+
int index_comma = pattern.indexOf(",");
531+
int cnt = index_comma - index_point - 1;
527532
String commas = (endsWithCommasMatcher.group(1));
528533
BigDecimal temp = BigDecimal.ONE;
529534
for (int i = 0; i < commas.length(); ++i) {
530535
temp = temp.multiply(ONE_THOUSAND);
531536
}
537+
for (int i = 0; i < cnt ; i++) {
538+
temp = temp.multiply(TEN);
539+
}
532540
divider = temp;
533541
} else {
534542
divider = null;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.idev.excel.test.temp.issue2319;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
@EqualsAndHashCode
10+
public class Issue2319 {
11+
private String num1;
12+
private String num2;
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.idev.excel.test.temp.issue2319;
2+
3+
import java.io.File;
4+
5+
import com.alibaba.fastjson2.JSON;
6+
7+
import cn.idev.excel.EasyExcel;
8+
import cn.idev.excel.FastExcel;
9+
import cn.idev.excel.read.listener.PageReadListener;
10+
import cn.idev.excel.test.util.TestFileUtil;
11+
import lombok.extern.slf4j.Slf4j;
12+
import org.junit.jupiter.api.Test;
13+
14+
@Slf4j
15+
public class Issue2319Test {
16+
@Test
17+
public void IssueTest1() {
18+
String fileName = TestFileUtil.getPath() + "temp/issue2319" + File.separator + "test1.xlsx";
19+
FastExcel.read(fileName, Issue2319.class, new PageReadListener<Issue2319>(dataList -> {
20+
for (Issue2319 issueData : dataList) {
21+
System.out.println(("读取到一条数据{}" + JSON.toJSONString(issueData)));
22+
}
23+
})).sheet().doRead();
24+
}
25+
26+
//CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2319
27+
@Test
28+
public void IssueTest2() {
29+
String fileName = TestFileUtil.getPath() + "temp/issue2319" + File.separator + "test2.xlsx";
30+
FastExcel.read(fileName, Issue2319.class, new PageReadListener<Issue2319>(dataList -> {
31+
for (Issue2319 issueData : dataList) {
32+
System.out.println(("读取到一条数据{}" + JSON.toJSONString(issueData)));
33+
}
34+
})).sheet().doRead();
35+
}
36+
}
Binary file not shown.
Binary file not shown.

update.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
具体更新内容如下:
55
- 【改进】移除 `itext` 依赖库,将 `转换PDF` 功能迁移至新项目;
66
- 【修复】fill填充空数据,可能导致行数据错乱的问题;
7-
- 【修复】打印CSV文件的 `hashcode`,可能会产生的堆栈溢出问题
7+
- 【修复】自定义数据格式可能导致数据读取失败的问题
88
- 【优化】增加报错内容详细信息;
99
- 【优化】更新代码格式和部分错别字。

0 commit comments

Comments
 (0)