Browse Source

Merge pull request #592 from alibaba/2.0.0-beta6

2.0.0 beta6
bugfix v2.0.0
Jiaju Zhuang 5 years ago committed by GitHub
parent
commit
0530b09c99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pom.xml
  2. 34
      quickstart.md
  3. 4
      src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java
  4. 4
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
  5. 17
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  6. 4
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java
  7. 12
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java
  8. 6
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java
  9. 16
      src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java
  10. 6
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java
  11. 6
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java
  12. 6
      src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java
  13. 6
      src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java
  14. 6
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java
  15. 15
      src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
  16. 24
      src/main/java/com/alibaba/excel/metadata/CellData.java
  17. 2
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  18. 2
      src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java
  19. 3
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
  20. 1
      src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java
  21. 11
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  22. 2
      src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java
  23. 5
      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.0.0-beta6</version> <version>2.0.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

34
quickstart.md

@ -7,6 +7,8 @@
* 单个文件的并发写入、读取 * 单个文件的并发写入、读取
* 读取图片 * 读取图片
* 宏 * 宏
#### 关于版本兼容
目前poi用的 4.0.1 建议检查是否该版本。如果看到`NoClassDefFoundError`或者`ClassNotFoundException`,请查看poi相关版本是否都为4.0.1。
#### 详细参数介绍 #### 详细参数介绍
有些参数不知道怎么用,或者有些功能不知道用什么参数,参照:[详细参数介绍](/docs/API.md) 有些参数不知道怎么用,或者有些功能不知道用什么参数,参照:[详细参数介绍](/docs/API.md)
#### 开源项目不容易,如果觉得本项目对您的工作还是有帮助的话,请在右上角帮忙点个★Star。 #### 开源项目不容易,如果觉得本项目对您的工作还是有帮助的话,请在右上角帮忙点个★Star。
@ -573,20 +575,26 @@ public class ComplexHeadData {
```java ```java
/** /**
* 重复多次写入 * 重复多次写入
* <p>1. 创建excel对应的实体对象 参照{@link ComplexHeadData} * <p>
* <p>2. 使用{@link ExcelProperty}注解指定复杂的头 * 1. 创建excel对应的实体对象 参照{@link ComplexHeadData}
* <p>3. 直接调用二次写入即可 * <p>
* 2. 使用{@link ExcelProperty}注解指定复杂的头
* <p>
* 3. 直接调用二次写入即可
*/ */
@Test @Test
public void repeatedWrite() { public void repeatedWrite() {
String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读 // 这里 需要指定写用哪个class去读
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
// 这里注意 如果同一个sheet只要创建一次
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
// 第一次写入会创建头 // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来
excelWriter.write(data(), writeSheet); for (int i = 0; i < 5; i++) {
// 第二次写入会在上一次写入的最后一行后面写入 // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
excelWriter.write(data(), writeSheet); List<DemoData> data = data();
excelWriter.write(data, writeSheet);
}
/// 千万别忘记finish 会帮忙关闭流 /// 千万别忘记finish 会帮忙关闭流
excelWriter.finish(); excelWriter.finish();
} }
@ -1038,14 +1046,18 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
参照:[对象](#simpleWriteObject) 就是名称变了下 参照:[对象](#simpleWriteObject) 就是名称变了下
##### 代码 ##### 代码
```java ```java
/** /**
* 文件下载 * 文件下载
* <p>1. 创建excel对应的实体对象 参照{@link DownloadData} * <p>
* <p>2. 设置返回的 参数 * 1. 创建excel对应的实体对象 参照{@link DownloadData}
* <p>3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大 * <p>
* 2. 设置返回的 参数
* <p>
* 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大
*/ */
@GetMapping("download") @GetMapping("download")
public void download(HttpServletResponse response) throws IOException { public void download(HttpServletResponse response) throws IOException {
// 这里注意 有同学反应下载的文件名不对。这个时候 请别使用swagger 他会影像
response.setContentType("application/vnd.ms-excel"); response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");

4
src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java

@ -1,5 +1,7 @@
package com.alibaba.excel.analysis.v03.handlers; package com.alibaba.excel.analysis.v03.handlers;
import java.math.BigDecimal;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.model.HSSFFormulaParser; import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.FormulaRecord;
@ -66,7 +68,7 @@ public class FormulaRecordHandler extends AbstractXlsRecordHandler {
tempCellData.setFormulaValue(formulaValue); tempCellData.setFormulaValue(formulaValue);
break; break;
case NUMERIC: case NUMERIC:
this.cellData = new CellData(frec.getValue()); this.cellData = new CellData(BigDecimal.valueOf(frec.getValue()));
this.cellData.setFormula(Boolean.TRUE); this.cellData.setFormula(Boolean.TRUE);
this.cellData.setFormulaValue(formulaValue); this.cellData.setFormulaValue(formulaValue);
break; break;

4
src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java

@ -1,5 +1,7 @@
package com.alibaba.excel.analysis.v03.handlers; package com.alibaba.excel.analysis.v03.handlers;
import java.math.BigDecimal;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
@ -29,7 +31,7 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler {
NumberRecord numrec = (NumberRecord)record; NumberRecord numrec = (NumberRecord)record;
this.row = numrec.getRow(); this.row = numrec.getRow();
this.column = numrec.getColumn(); this.column = numrec.getColumn();
this.cellData = new CellData(numrec.getValue()); this.cellData = new CellData(BigDecimal.valueOf(numrec.getValue()));
this.cellData.setDataFormat(formatListener.getFormatIndex(numrec)); this.cellData.setDataFormat(formatListener.getFormatIndex(numrec));
this.cellData.setDataFormatString(formatListener.getFormatString(numrec)); this.cellData.setDataFormatString(formatListener.getFormatString(numrec));
} }

17
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java

@ -7,6 +7,9 @@ import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_TAG;
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG;
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG;
import java.math.BigDecimal;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -33,8 +36,7 @@ import com.alibaba.excel.util.StringUtils;
*/ */
public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder { public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder {
private final AnalysisContext analysisContext; private final AnalysisContext analysisContext;
private String currentTag; private Deque<String> currentTagDeque = new LinkedList<String>();
private String currentCellIndex;
private int curCol; private int curCol;
private Map<Integer, CellData> curRowContent = new TreeMap<Integer, CellData>(); private Map<Integer, CellData> curRowContent = new TreeMap<Integer, CellData>();
private CellData currentCellData; private CellData currentCellData;
@ -61,11 +63,10 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void startHandle(String name, Attributes attributes) { public void startHandle(String name, Attributes attributes) {
currentTag = name; currentTagDeque.push(name);
// start a cell // start a cell
if (CELL_TAG.equals(name)) { if (CELL_TAG.equals(name)) {
currentCellIndex = attributes.getValue(ExcelXmlConstants.POSITION); curCol = PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION));
curCol = PositionUtils.getCol(currentCellIndex);
// t="s" ,it's means String // t="s" ,it's means String
// t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml' // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
@ -100,6 +101,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void endHandle(String name) { public void endHandle(String name) {
currentTagDeque.pop();
if (CELL_VALUE_TAG.equals(name)) { if (CELL_VALUE_TAG.equals(name)) {
// Have to go "sharedStrings.xml" and get it // Have to go "sharedStrings.xml" and get it
if (currentCellData.getType() == CellDataTypeEnum.STRING) { if (currentCellData.getType() == CellDataTypeEnum.STRING) {
@ -133,6 +135,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
if (StringUtils.isEmpty(currentCellValue)) { if (StringUtils.isEmpty(currentCellValue)) {
return; return;
} }
String currentTag = currentTagDeque.peek();
if (currentTag == null) { if (currentTag == null) {
return; return;
} }
@ -162,8 +165,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
case NUMBER: case NUMBER:
case EMPTY: case EMPTY:
currentCellData.setType(CellDataTypeEnum.NUMBER); currentCellData.setType(CellDataTypeEnum.NUMBER);
if (currentCellData.getDoubleValue() == null) { if (currentCellData.getNumberValue() == null) {
currentCellData.setDoubleValue(Double.valueOf(currentCellValue)); currentCellData.setNumberValue(new BigDecimal(currentCellValue));
} }
break; break;
default: default:

4
src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java

@ -28,12 +28,12 @@ public class BigDecimalNumberConverter implements Converter<BigDecimal> {
@Override @Override
public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return BigDecimal.valueOf(cellData.getDoubleValue()); return cellData.getNumberValue();
} }
@Override @Override
public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty, public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(value);
} }
} }

12
src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java

@ -1,5 +1,7 @@
package com.alibaba.excel.converters.booleanconverter; package com.alibaba.excel.converters.booleanconverter;
import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -12,10 +14,6 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class BooleanNumberConverter implements Converter<Boolean> { public class BooleanNumberConverter implements Converter<Boolean> {
private static final Double ONE = 1.0;
private static final Double ZERO = 0.0;
@Override @Override
public Class supportJavaTypeKey() { public Class supportJavaTypeKey() {
return Boolean.class; return Boolean.class;
@ -29,7 +27,7 @@ public class BooleanNumberConverter implements Converter<Boolean> {
@Override @Override
public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
if (ONE.equals(cellData.getDoubleValue())) { if (BigDecimal.ONE.compareTo(cellData.getNumberValue()) == 0) {
return Boolean.TRUE; return Boolean.TRUE;
} }
return Boolean.FALSE; return Boolean.FALSE;
@ -39,9 +37,9 @@ public class BooleanNumberConverter implements Converter<Boolean> {
public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
if (value) { if (value) {
return new CellData(ONE); return new CellData(BigDecimal.ONE);
} }
return new CellData(ZERO); return new CellData(BigDecimal.ZERO);
} }
} }

6
src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java

@ -1,5 +1,7 @@
package com.alibaba.excel.converters.byteconverter; package com.alibaba.excel.converters.byteconverter;
import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -26,13 +28,13 @@ public class ByteNumberConverter implements Converter<Byte> {
@Override @Override
public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().byteValue(); return cellData.getNumberValue().byteValue();
} }
@Override @Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(BigDecimal.valueOf(value));
} }
} }

16
src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java

@ -1,8 +1,9 @@
package com.alibaba.excel.converters.date; package com.alibaba.excel.converters.date;
import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
@ -31,9 +32,10 @@ public class DateNumberConverter implements Converter<Date> {
public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), globalConfiguration.getUse1904windowing(), null); return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing(), null);
} else { } else {
return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null); contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);
} }
} }
@ -41,6 +43,12 @@ public class DateNumberConverter implements Converter<Date> {
@Override @Override
public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData((double)(value.getTime())); if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new CellData(
BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing())));
} else {
return new CellData(BigDecimal.valueOf(
DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing())));
}
} }
} }

6
src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java

@ -1,5 +1,7 @@
package com.alibaba.excel.converters.doubleconverter; package com.alibaba.excel.converters.doubleconverter;
import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -26,13 +28,13 @@ public class DoubleNumberConverter implements Converter<Double> {
@Override @Override
public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue(); return cellData.getNumberValue().doubleValue();
} }
@Override @Override
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(value); return new CellData(BigDecimal.valueOf(value));
} }
} }

6
src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java

@ -1,5 +1,7 @@
package com.alibaba.excel.converters.floatconverter; package com.alibaba.excel.converters.floatconverter;
import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -26,13 +28,13 @@ public class FloatNumberConverter implements Converter<Float> {
@Override @Override
public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().floatValue(); return cellData.getNumberValue().floatValue();
} }
@Override @Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(BigDecimal.valueOf(value));
} }
} }

6
src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java

@ -1,5 +1,7 @@
package com.alibaba.excel.converters.integer; package com.alibaba.excel.converters.integer;
import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -26,13 +28,13 @@ public class IntegerNumberConverter implements Converter<Integer> {
@Override @Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().intValue(); return cellData.getNumberValue().intValue();
} }
@Override @Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(BigDecimal.valueOf(value));
} }
} }

6
src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java

@ -1,5 +1,7 @@
package com.alibaba.excel.converters.longconverter; package com.alibaba.excel.converters.longconverter;
import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -26,13 +28,13 @@ public class LongNumberConverter implements Converter<Long> {
@Override @Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().longValue(); return cellData.getNumberValue().longValue();
} }
@Override @Override
public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(BigDecimal.valueOf(value));
} }
} }

6
src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java

@ -1,5 +1,7 @@
package com.alibaba.excel.converters.shortconverter; package com.alibaba.excel.converters.shortconverter;
import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -26,13 +28,13 @@ public class ShortNumberConverter implements Converter<Short> {
@Override @Override
public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().shortValue(); return cellData.getNumberValue().shortValue();
} }
@Override @Override
public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty, public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(BigDecimal.valueOf(value));
} }
} }

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

@ -1,6 +1,7 @@
package com.alibaba.excel.converters.string; package com.alibaba.excel.converters.string;
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import java.math.BigDecimal;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
@ -34,30 +35,30 @@ public class StringNumberConverter implements Converter<String> {
// If there are "DateTimeFormat", read as date // If there are "DateTimeFormat", read as date
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) { if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
return DateUtils.format( return DateUtils.format(
HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null), contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
contentProperty.getDateTimeFormatProperty().getFormat()); contentProperty.getDateTimeFormatProperty().getFormat());
} }
// If there are "NumberFormat", read as number // If there are "NumberFormat", read as number
if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) { if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
return NumberUtils.format(cellData.getDoubleValue(), contentProperty); return NumberUtils.format(cellData.getNumberValue(), contentProperty);
} }
// Excel defines formatting // Excel defines formatting
if (cellData.getDataFormat() != null) { if (cellData.getDataFormat() != null) {
if (DateUtil.isADateFormat(cellData.getDataFormat(), cellData.getDataFormatString())) { if (DateUtil.isADateFormat(cellData.getDataFormat(), cellData.getDataFormatString())) {
return DateUtils.format(HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), return DateUtils.format(DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing(), null)); globalConfiguration.getUse1904windowing(), null));
} else { } else {
return NumberUtils.format(cellData.getDoubleValue(), contentProperty); return NumberUtils.format(cellData.getNumberValue(), contentProperty);
} }
} }
// Default conversion number // Default conversion number
return NumberUtils.format(cellData.getDoubleValue(), contentProperty); return NumberUtils.format(cellData.getNumberValue(), contentProperty);
} }
@Override @Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
return new CellData(Double.valueOf(value)); return new CellData(new BigDecimal(value));
} }
} }

24
src/main/java/com/alibaba/excel/metadata/CellData.java

@ -1,5 +1,7 @@
package com.alibaba.excel.metadata; package com.alibaba.excel.metadata;
import java.math.BigDecimal;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
@ -13,7 +15,7 @@ public class CellData {
/** /**
* {@link CellDataTypeEnum#NUMBER} * {@link CellDataTypeEnum#NUMBER}
*/ */
private Double doubleValue; private BigDecimal numberValue;
/** /**
* {@link CellDataTypeEnum#STRING} and{@link CellDataTypeEnum#ERROR} * {@link CellDataTypeEnum#STRING} and{@link CellDataTypeEnum#ERROR}
*/ */
@ -36,7 +38,7 @@ public class CellData {
public CellData(CellData other) { public CellData(CellData other) {
this.type = other.type; this.type = other.type;
this.doubleValue = other.doubleValue; this.numberValue = other.numberValue;
this.stringValue = other.stringValue; this.stringValue = other.stringValue;
this.booleanValue = other.booleanValue; this.booleanValue = other.booleanValue;
this.formula = other.formula; this.formula = other.formula;
@ -62,12 +64,12 @@ public class CellData {
this.formula = Boolean.FALSE; this.formula = Boolean.FALSE;
} }
public CellData(Double doubleValue) { public CellData(BigDecimal numberValue) {
if (doubleValue == null) { if (numberValue == null) {
throw new IllegalArgumentException("DoubleValue can not be null"); throw new IllegalArgumentException("DoubleValue can not be null");
} }
this.type = CellDataTypeEnum.NUMBER; this.type = CellDataTypeEnum.NUMBER;
this.doubleValue = doubleValue; this.numberValue = numberValue;
this.formula = Boolean.FALSE; this.formula = Boolean.FALSE;
} }
@ -105,12 +107,12 @@ public class CellData {
this.type = type; this.type = type;
} }
public Double getDoubleValue() { public BigDecimal getNumberValue() {
return doubleValue; return numberValue;
} }
public void setDoubleValue(Double doubleValue) { public void setNumberValue(BigDecimal numberValue) {
this.doubleValue = doubleValue; this.numberValue = numberValue;
} }
public String getStringValue() { public String getStringValue() {
@ -181,7 +183,7 @@ public class CellData {
} }
return; return;
case NUMBER: case NUMBER:
if (doubleValue == null) { if (numberValue == null) {
type = CellDataTypeEnum.EMPTY; type = CellDataTypeEnum.EMPTY;
} }
return; return;
@ -198,7 +200,7 @@ public class CellData {
public String toString() { public String toString() {
switch (type) { switch (type) {
case NUMBER: case NUMBER:
return doubleValue.toString(); return numberValue.toString();
case BOOLEAN: case BOOLEAN:
return booleanValue.toString(); return booleanValue.toString();
case STRING: case STRING:

2
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java

@ -312,7 +312,7 @@ public class ExcelBuilderImpl implements ExcelBuilder {
cell.setCellValue(cellData.getBooleanValue()); cell.setCellValue(cellData.getBooleanValue());
return cellData; return cellData;
case NUMBER: case NUMBER:
cell.setCellValue(cellData.getDoubleValue()); cell.setCellValue(cellData.getNumberValue().doubleValue());
return cellData; return cellData;
case IMAGE: case IMAGE:
setImageValue(cellData, cell); setImageValue(cellData, cell);

2
src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java

@ -58,7 +58,7 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty
case BOOLEAN: case BOOLEAN:
return cellData.getBooleanValue().toString().getBytes().length; return cellData.getBooleanValue().toString().getBytes().length;
case NUMBER: case NUMBER:
return cellData.getDoubleValue().toString().getBytes().length; return cellData.getNumberValue().toString().getBytes().length;
default: default:
return -1; return -1;
} }

3
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java

@ -65,7 +65,8 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
Assert.assertEquals(data.getStringString(), "测试"); Assert.assertEquals(data.getStringString(), "测试");
Assert.assertEquals(data.getStringError(), "#VALUE!"); Assert.assertEquals(data.getStringError(), "#VALUE!");
Assert.assertEquals(data.getStringNumberDate(), "2020-01-01 01:01:01"); Assert.assertEquals(data.getStringNumberDate(), "2020-01-01 01:01:01");
Assert.assertEquals(data.getStringFormulaNumber(), "2.0"); double doubleStringFormulaNumber = new BigDecimal(data.getStringFormulaNumber()).doubleValue();
Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0);
Assert.assertEquals(data.getStringFormulaString(), "1测试"); Assert.assertEquals(data.getStringFormulaString(), "1测试");
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
} }

1
src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java

@ -33,6 +33,7 @@ public class WebTest {
*/ */
@GetMapping("download") @GetMapping("download")
public void download(HttpServletResponse response) throws IOException { public void download(HttpServletResponse response) throws IOException {
// 这里注意 有同学反应下载的文件名不对。这个时候 请别使用swagger 他会影像
response.setContentType("application/vnd.ms-excel"); response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");

11
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -108,11 +108,14 @@ public class WriteTest {
String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读 // 这里 需要指定写用哪个class去读
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
// 这里注意 如果同一个sheet只要创建一次
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
// 第一次写入会创建头 // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来
excelWriter.write(data(), writeSheet); for (int i = 0; i < 5; i++) {
// 第二次写入会在上一次写入的最后一行后面写入 // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
excelWriter.write(data(), writeSheet); List<DemoData> data = data();
excelWriter.write(data, writeSheet);
}
/// 千万别忘记finish 会帮忙关闭流 /// 千万别忘记finish 会帮忙关闭流
excelWriter.finish(); excelWriter.finish();
} }

2
src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

@ -24,7 +24,7 @@ public class HgTest {
@Test @Test
public void hh() throws IOException { public void hh() throws IOException {
List<Object> list = List<Object> list =
EasyExcel.read(new FileInputStream("D:\\test\\商户不匹配工单信息收集表格.xlsx")).headRowNumber(0).sheet().doReadSync(); EasyExcel.read(new FileInputStream("D:\\test\\number.xlsx")).headRowNumber(0).sheet().doReadSync();
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }

5
update.md

@ -1,3 +1,8 @@
# 2.0.0
* 修复当cell为空可能会抛出空指针的bug
* 修复电话等长数字可能出现科学计数法的问题 [Issue #583](https://github.com/alibaba/easyexcel/issues/583)
* 升级为正式版
# 2.0.0-beta6 # 2.0.0-beta6
* 修复空行读取空指针异常 * 修复空行读取空指针异常
* 修复写入指定头为List<List<String>>,但是数据用List<Class>导致的空指针 * 修复写入指定头为List<List<String>>,但是数据用List<Class>导致的空指针

Loading…
Cancel
Save