Browse Source

Merge branch 'master' into developing

# Conflicts:
#	pom.xml
#	src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
#	src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
#	src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java
#	src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
#	src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
#	update.md
developing
Jiaju Zhuang 3 years ago
parent
commit
5dd3bc98f6
  1. 8
      README.md
  2. 2
      src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java
  3. 2
      src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
  4. 8
      src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
  5. 4
      src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java
  6. 19
      src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
  7. 13
      src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
  8. BIN
      src/test/resources/dataformat/dataformat.xls
  9. BIN
      src/test/resources/dataformat/dataformat.xlsx
  10. 6
      update.md

8
README.md

@ -5,11 +5,13 @@ EasyExcel
[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html) [![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
[QQ1群(已满): 662022184](https://jq.qq.com/?_wv=1027&k=1T21jJxh) [QQ1群(已满): 662022184](https://jq.qq.com/?_wv=1027&k=1T21jJxh)
[QQ2群: 1097936804](https://jq.qq.com/?_wv=1027&k=j5zEy6Xl) [QQ2群(已满): 1097936804](https://jq.qq.com/?_wv=1027&k=j5zEy6Xl)
[QQ3群: 453928496](https://qm.qq.com/cgi-bin/qm/qr?k=e2ULsA5A0GldhV2CXJ8sIbAyu9I6qqs7&jump_from=webapi)
[钉钉1群(已满): 21960511](https://qr.dingtalk.com/action/joingroup?code=v1,k1,cchz6k12ci9B08NNqhNRFGXocNVHrZtW0kaOtTKg/Rk=&_dt_no_comment=1&origin=11) [钉钉1群(已满): 21960511](https://qr.dingtalk.com/action/joingroup?code=v1,k1,cchz6k12ci9B08NNqhNRFGXocNVHrZtW0kaOtTKg/Rk=&_dt_no_comment=1&origin=11)
[钉钉2群(已满): 32796397](https://qr.dingtalk.com/action/joingroup?code=v1,k1,jyU9GtEuNU5S0QTyklqYcYJ8qDZtUuTPMM7uPZTS8Hs=&_dt_no_comment=1&origin=11) [钉钉2群(已满): 32796397](https://qr.dingtalk.com/action/joingroup?code=v1,k1,jyU9GtEuNU5S0QTyklqYcYJ8qDZtUuTPMM7uPZTS8Hs=&_dt_no_comment=1&origin=11)
[钉钉3群(已满): 33797247](https://qr.dingtalk.com/action/joingroup?code=v1,k1,3UGlEScTGQaHpW2cIRo+gkxJ9EVZ5fz26M6nW3uFP30=&_dt_no_comment=1&origin=11) [钉钉3群(已满): 33797247](https://qr.dingtalk.com/action/joingroup?code=v1,k1,3UGlEScTGQaHpW2cIRo+gkxJ9EVZ5fz26M6nW3uFP30=&_dt_no_comment=1&origin=11)
[钉钉4群: 33491624](https://qr.dingtalk.com/action/joingroup?code=v1,k1,V14Pb65Too70rQkEaJ9ohb6lZBZbtp6jIL/q9EWh9vA=&_dt_no_comment=1&origin=11) [钉钉4群(已满): 33491624](https://qr.dingtalk.com/action/joingroup?code=v1,k1,V14Pb65Too70rQkEaJ9ohb6lZBZbtp6jIL/q9EWh9vA=&_dt_no_comment=1&origin=11)
[钉钉5群: 32134498](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingb9fa1325d9dccc3ecac589edd02f1650&5233a=71a83&cbdbhh=qwertyuiop)
[官方网站: https://yuque.com/easyexcel](https://www.yuque.com/easyexcel/doc/easyexcel) [官方网站: https://yuque.com/easyexcel](https://www.yuque.com/easyexcel/doc/easyexcel)
[常见问题](https://www.yuque.com/easyexcel/faq) [常见问题](https://www.yuque.com/easyexcel/faq)
@ -29,7 +31,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
* [贡献代码](https://www.yuque.com/easyexcel/doc/contribute) * [贡献代码](https://www.yuque.com/easyexcel/doc/contribute)
## 维护者 ## 维护者
玉霄、庄家钜、怀宇 姬朋飞(玉霄)、庄家钜、怀宇
## 快速开始 ## 快速开始
### 读Excel ### 读Excel
DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/demo/read/ReadTest.java](/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java) DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/demo/read/ReadTest.java](/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java)

2
src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java

@ -43,7 +43,7 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler
break; break;
} }
tempCellData.setType(CellDataTypeEnum.NUMBER); tempCellData.setType(CellDataTypeEnum.NUMBER);
tempCellData.setNumberValue(new BigDecimal(tempData.toString())); tempCellData.setNumberValue(BigDecimal.valueOf(Double.parseDouble(tempDataString)));
break; break;
default: default:
throw new IllegalStateException("Cannot set values now"); throw new IllegalStateException("Cannot set values now");

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

@ -51,7 +51,7 @@ public class StringNumberConverter implements Converter<String> {
&& cellData.getDataFormatData().getIndex() != null && !StringUtils.isEmpty( && cellData.getDataFormatData().getIndex() != null && !StringUtils.isEmpty(
cellData.getDataFormatData().getFormat()); cellData.getDataFormatData().getFormat());
if (hasDataFormatData) { if (hasDataFormatData) {
return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), return NumberDataFormatterUtils.format(cellData.getNumberValue(),
cellData.getDataFormatData().getIndex(), cellData.getDataFormatData().getFormat(), globalConfiguration); cellData.getDataFormatData().getIndex(), cellData.getDataFormatData().getFormat(), globalConfiguration);
} }
// Default conversion number // Default conversion number

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

@ -642,8 +642,8 @@ public class DataFormatter {
* @param dataFormatString * @param dataFormatString
* @return a formatted number string * @return a formatted number string
*/ */
private String getFormattedNumberString(Double data, Short dataFormat, String dataFormatString) { private String getFormattedNumberString(BigDecimal data, Short 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
} }
@ -656,9 +656,9 @@ public class DataFormatter {
* @param dataFormatString * @param dataFormatString
* @return * @return
*/ */
public String format(Double data, Short dataFormat, String dataFormatString) { public String format(BigDecimal data, Short 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

@ -2,6 +2,8 @@ package com.alibaba.excel.util;
import java.util.Locale; import java.util.Locale;
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;
@ -26,7 +28,7 @@ public class NumberDataFormatterUtils {
* @param globalConfiguration * @param globalConfiguration
* @return * @return
*/ */
public static String format(Double data, Short dataFormat, String dataFormatString, public static String format(BigDecimal data, Short dataFormat, String dataFormatString,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
if (globalConfiguration == null) { if (globalConfiguration == null) {
return format(data, dataFormat, dataFormatString, null, null, null); return format(data, dataFormat, dataFormatString, null, null, null);

19
src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java

@ -13,6 +13,9 @@ import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@ -43,6 +46,14 @@ public class DateFormatTest {
private void readCn(File file) { private void readCn(File file) {
List<DateFormatData> list = List<DateFormatData> list =
EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync(); EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync();
for (DateFormatData data : list) {
if (data.getDateStringCn() != null && !data.getDateStringCn().equals(data.getDate())) {
LOGGER.info("date:cn:{},{}", data.getDateStringCn(), data.getDate());
}
if (data.getNumberStringCn() != null && !data.getNumberStringCn().equals(data.getNumber())) {
LOGGER.info("number:cn{},{}", data.getNumberStringCn(), data.getNumber());
}
}
for (DateFormatData data : list) { for (DateFormatData data : list) {
Assert.assertEquals(data.getDateStringCn(), data.getDate()); Assert.assertEquals(data.getDateStringCn(), data.getDate());
Assert.assertEquals(data.getNumberStringCn(), data.getNumber()); Assert.assertEquals(data.getNumberStringCn(), data.getNumber());
@ -52,6 +63,14 @@ public class DateFormatTest {
private void readUs(File file) { private void readUs(File file) {
List<DateFormatData> list = List<DateFormatData> list =
EasyExcel.read(file, DateFormatData.class, null).locale(Locale.US).sheet().doReadSync(); EasyExcel.read(file, DateFormatData.class, null).locale(Locale.US).sheet().doReadSync();
for (DateFormatData data : list) {
if (data.getDateStringUs() != null && !data.getDateStringUs().equals(data.getDate())) {
LOGGER.info("date:us:{},{}", data.getDateStringUs(), data.getDate());
}
if (data.getNumberStringUs() != null && !data.getNumberStringUs().equals(data.getNumber())) {
LOGGER.info("number:us{},{}", data.getNumberStringUs(), data.getNumber());
}
}
for (DateFormatData data : list) { for (DateFormatData data : list) {
Assert.assertEquals(data.getDateStringUs(), data.getDate()); Assert.assertEquals(data.getDateStringUs(), data.getDate());
Assert.assertEquals(data.getNumberStringUs(), data.getNumber()); Assert.assertEquals(data.getNumberStringUs(), data.getNumber());

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

@ -3,6 +3,9 @@ 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;
@ -17,7 +20,6 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; 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");

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

Binary file not shown.

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

Binary file not shown.

6
update.md

@ -1,3 +1,9 @@
# 2.2.10
* 修复读取的时候用string接收数字 可能四舍五入不一致的bug
# 2.2.9
* 修复读取的时候用string接收数字 可能四舍五入不一致的bug
# 3.0.0-beta1 # 3.0.0-beta1
* 升级jdk8 不再支持jdk6 jdk7 * 升级jdk8 不再支持jdk6 jdk7
* 升级poi 到 4.1.2 * 升级poi 到 4.1.2

Loading…
Cancel
Save