Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix#2443:修复int读取带小数点整数错误的问题 #2445

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/main/java/com/alibaba/excel/util/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,27 @@ public static Long parseLong(String string, ExcelContentProperty contentProperty
}

/**
* parse
* parse Integer from string
*
* @param string
* @param contentProperty
* @return
* @param string An integer read in string format
* @param contentProperty Properties of the content read in
* @return An integer converted from a string
*/
public static Integer parseInteger(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
// CS304 Issue link: https://github.com/alibaba/easyexcel/issues/2443
int stringLength = string.length();
if(stringLength>0){
int pointer = stringLength;
for(int i=0;i<stringLength;i++){
if(string.charAt(i)=='.'){
pointer = i;
break;
}
}
string = string.substring(0,pointer);
}

return Integer.valueOf(string);
}
return parse(string, contentProperty).intValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.alibaba.easyexcel.test.temp.issue2443;

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

import java.util.Date;

@Getter
@Setter
@EqualsAndHashCode
public class Issue2443 {
private int a;
private int b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.alibaba.easyexcel.test.temp.issue2443;

import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.File;
import java.text.ParseException;

@Ignore
@Slf4j
public class Issue2443Test {
//CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2443
@Test
public void IssueTest1() {
String fileName = TestFileUtil.getPath() + "temp/issue2443" + File.separator + "date1.xlsx";
EasyExcel.read(fileName, Issue2443.class, new PageReadListener<Issue2443>(dataList -> {
for (Issue2443 issueData : dataList) {
log.info("读取到一条数据{}", JSON.toJSONString(issueData));
}
})).sheet().doRead();
}
//CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2443
@Test
public void IssueTest2() {
String fileName = TestFileUtil.getPath() + "temp/issue2443" + File.separator + "date2.xlsx";
EasyExcel.read(fileName, Issue2443.class, new PageReadListener<Issue2443>(dataList -> {
for (Issue2443 issueData : dataList) {
log.info("读取到一条数据{}", JSON.toJSONString(issueData));
}
})).sheet().doRead();
}

@Test
public void parseIntegerTest1() throws ParseException {
String string = "1.00";
ExcelContentProperty contentProperty = null;
int Int = NumberUtils.parseInteger(string,contentProperty);
assertEquals(1, Int);
}

@Test
public void parseIntegerTest2() throws ParseException {
String string = "2.00";
ExcelContentProperty contentProperty = null;
int Int = NumberUtils.parseInteger(string,contentProperty);
assertEquals(2, Int);
}

}
Binary file added src/test/resources/temp/issue2443/date1.xlsx
Binary file not shown.
Binary file added src/test/resources/temp/issue2443/date2.xlsx
Binary file not shown.