Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: Fix Potential Data Read Failures Caused by Custom Data Format…
Browse files Browse the repository at this point in the history
…s.by @zyd0131 at #2319
psxjoy committed Dec 23, 2024
1 parent 2e10c3b commit 82f31cb
Showing 6 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -118,6 +118,8 @@ public class DataFormatter {
*/
private static final String invalidDateTimeString;

private static final BigDecimal TEN = new BigDecimal(10);

static {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < 255; i++) {buf.append('#');}
@@ -524,11 +526,17 @@ public InternalDecimalFormatWithScale(String pattern, DecimalFormatSymbols symbo
setExcelStyleRoundingMode(df);
Matcher endsWithCommasMatcher = endsWithCommas.matcher(pattern);
if (endsWithCommasMatcher.find()) {
int index_point = pattern.indexOf(".");
int index_comma = pattern.indexOf(",");
int cnt = index_comma - index_point - 1;
String commas = (endsWithCommasMatcher.group(1));
BigDecimal temp = BigDecimal.ONE;
for (int i = 0; i < commas.length(); ++i) {
temp = temp.multiply(ONE_THOUSAND);
}
for (int i = 0; i < cnt ; i++) {
temp = temp.multiply(TEN);
}
divider = temp;
} else {
divider = null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.idev.excel.test.temp.issue2319;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@EqualsAndHashCode
public class Issue2319 {
private String num1;
private String num2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.idev.excel.test.temp.issue2319;

import java.io.File;

import com.alibaba.fastjson2.JSON;

import cn.idev.excel.EasyExcel;
import cn.idev.excel.FastExcel;
import cn.idev.excel.read.listener.PageReadListener;
import cn.idev.excel.test.util.TestFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

@Slf4j
public class Issue2319Test {
@Test
public void IssueTest1() {
String fileName = TestFileUtil.getPath() + "temp/issue2319" + File.separator + "test1.xlsx";
FastExcel.read(fileName, Issue2319.class, new PageReadListener<Issue2319>(dataList -> {
for (Issue2319 issueData : dataList) {
System.out.println(("读取到一条数据{}" + JSON.toJSONString(issueData)));
}
})).sheet().doRead();
}

//CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2319
@Test
public void IssueTest2() {
String fileName = TestFileUtil.getPath() + "temp/issue2319" + File.separator + "test2.xlsx";
FastExcel.read(fileName, Issue2319.class, new PageReadListener<Issue2319>(dataList -> {
for (Issue2319 issueData : dataList) {
System.out.println(("读取到一条数据{}" + JSON.toJSONString(issueData)));
}
})).sheet().doRead();
}
}
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion update.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@
具体更新内容如下:
- 【改进】移除 `itext` 依赖库,将 `转换PDF` 功能迁移至新项目;
- 【修复】fill填充空数据,可能导致行数据错乱的问题;
- 【修复】打印CSV文件的 `hashcode`,可能会产生的堆栈溢出问题
- 【修复】自定义数据格式可能导致数据读取失败的问题
- 【优化】增加报错内容详细信息;
- 【优化】更新代码格式和部分错别字。

0 comments on commit 82f31cb

Please sign in to comment.