Browse Source

修复读取的时候用string接收数字 可能四舍五入不一致的bug

pull/1922/head
是仪 4 years ago
parent
commit
995a96f15a
  1. 2
      pom.xml
  2. 2
      src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
  3. 8
      src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
  4. 4
      src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java
  5. 23
      src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
  6. 2
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  7. BIN
      src/test/resources/dataformat/dataformat.xls
  8. BIN
      src/test/resources/dataformat/dataformat.xlsx
  9. 3
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.2.8</version> <version>2.2.9</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

2
src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java

@ -47,7 +47,7 @@ public class StringNumberConverter implements Converter<String> {
} }
// Excel defines formatting // Excel defines formatting
if (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) { if (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) {
return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), cellData.getDataFormat(), return NumberDataFormatterUtils.format(cellData.getNumberValue(), cellData.getDataFormat(),
cellData.getDataFormatString(), globalConfiguration); cellData.getDataFormatString(), globalConfiguration);
} }
// Default conversion number // Default conversion number

8
src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java

@ -630,8 +630,8 @@ public class DataFormatter {
* @param dataFormatString * @param dataFormatString
* @return a formatted number string * @return a formatted number string
*/ */
private String getFormattedNumberString(Double data, Integer dataFormat, String dataFormatString) { private String getFormattedNumberString(BigDecimal data, Integer dataFormat, String dataFormatString) {
Format numberFormat = getFormat(data, dataFormat, dataFormatString); Format numberFormat = getFormat(data.doubleValue(), dataFormat, dataFormatString);
String formatted = numberFormat.format(data); String formatted = numberFormat.format(data);
return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
} }
@ -644,9 +644,9 @@ public class DataFormatter {
* @param dataFormatString * @param dataFormatString
* @return * @return
*/ */
public String format(Double data, Integer dataFormat, String dataFormatString) { public String format(BigDecimal data, Integer dataFormat, String dataFormatString) {
if (DateUtils.isADateFormat(dataFormat, dataFormatString)) { if (DateUtils.isADateFormat(dataFormat, dataFormatString)) {
return getFormattedDateString(data, dataFormat, dataFormatString); return getFormattedDateString(data.doubleValue(), dataFormat, dataFormatString);
} }
return getFormattedNumberString(data, dataFormat, dataFormatString); return getFormattedNumberString(data, dataFormat, dataFormatString);
} }

4
src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java

@ -1,5 +1,7 @@
package com.alibaba.excel.util; package com.alibaba.excel.util;
import java.math.BigDecimal;
import com.alibaba.excel.metadata.format.DataFormatter; import com.alibaba.excel.metadata.format.DataFormatter;
import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.GlobalConfiguration;
@ -24,7 +26,7 @@ public class NumberDataFormatterUtils {
* @param globalConfiguration * @param globalConfiguration
* @return * @return
*/ */
public static String format(Double data, Integer dataFormat, String dataFormatString, public static String format(BigDecimal data, Integer dataFormat, String dataFormatString,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
DataFormatter dataFormatter = DATA_FORMATTER_THREAD_LOCAL.get(); DataFormatter dataFormatter = DATA_FORMATTER_THREAD_LOCAL.get();
if (dataFormatter == null) { if (dataFormatter == null) {

23
src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java

@ -3,21 +3,23 @@ package com.alibaba.easyexcel.test.core.encrypt;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.core.simple.SimpleData; import com.alibaba.easyexcel.test.core.simple.SimpleData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/** /**
*
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -28,6 +30,15 @@ public class EncryptDataTest {
private static File file07OutputStream; private static File file07OutputStream;
private static File file03OutputStream; private static File file03OutputStream;
@Test
public void testformat() {
DecimalFormat decimalFormat = new DecimalFormat("0.00");
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
BigDecimal bigDecimal = new BigDecimal("0.105");
System.out.println(decimalFormat.format(bigDecimal));
}
@BeforeClass @BeforeClass
public static void init() { public static void init() {
file07 = TestFileUtil.createNewFile("encrypt07.xlsx"); file07 = TestFileUtil.createNewFile("encrypt07.xlsx");

2
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -34,7 +34,7 @@ public class Lock2Test {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
// File file = TestFileUtil.readUserHomeFile("test/test6.xls"); // File file = TestFileUtil.readUserHomeFile("test/test6.xls");
File file = new File("D:\\test\\T85_税金入库表202010.xlsx"); File file = new File("/Users/zhuangjiaju/Downloads/签到金模板-0507-v3.xlsx");
List<Object> list = EasyExcel.read(file).sheet(0).headRowNumber(0).doReadSync(); List<Object> list = EasyExcel.read(file).sheet(0).headRowNumber(0).doReadSync();
LOGGER.info("数据:{}", list.size()); LOGGER.info("数据:{}", list.size());

BIN
src/test/resources/dataformat/dataformat.xls

Binary file not shown.

BIN
src/test/resources/dataformat/dataformat.xlsx

Binary file not shown.

3
update.md

@ -1,3 +1,6 @@
# 2.2.9
* 修复读取的时候用string接收数字 可能四舍五入不一致的bug
# 2.2.8 # 2.2.8
* 兼容07在特殊的excel的情况下,读取数据异常 * 兼容07在特殊的excel的情况下,读取数据异常

Loading…
Cancel
Save