From a394a1706824094d3303183c6406d23a2ae4053b Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 10 Sep 2019 20:15:55 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9demo=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quickstart.md | 3 +++ .../java/com/alibaba/easyexcel/test/demo/write/WriteTest.java | 1 + 2 files changed, 4 insertions(+) diff --git a/quickstart.md b/quickstart.md index 27267d8..f9ca143 100644 --- a/quickstart.md +++ b/quickstart.md @@ -7,6 +7,8 @@ * 单个文件的并发写入、读取 * 读取图片 * 宏 +#### 关于版本兼容 +目前poi用的 4.0.1 建议检查是否该版本。如果出现找不到class之类的,八成就是这个原因。 #### 详细参数介绍 有些参数不知道怎么用,或者有些功能不知道用什么参数,参照:[详细参数介绍](/docs/API.md) #### 开源项目不容易,如果觉得本项目对您的工作还是有帮助的话,请在右上角帮忙点个★Star。 @@ -582,6 +584,7 @@ public class ComplexHeadData { String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去读 ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); + // 这里注意 如果同一个sheet只要创建一次 WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); // 第一次写入会创建头 excelWriter.write(data(), writeSheet); diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index e6bc998..094d1c1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -108,6 +108,7 @@ public class WriteTest { String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去读 ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); + // 这里注意 如果同一个sheet只要创建一次 WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); // 第一次写入会创建头 excelWriter.write(data(), writeSheet); From 1bfb8da93f12ed1def755f438499eb42ea207360 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 11 Sep 2019 18:48:38 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9demo=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quickstart.md | 31 ++++++++++++------- .../easyexcel/test/demo/web/WebTest.java | 1 + .../easyexcel/test/demo/write/WriteTest.java | 10 +++--- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/quickstart.md b/quickstart.md index f9ca143..fccc2a8 100644 --- a/quickstart.md +++ b/quickstart.md @@ -575,9 +575,12 @@ public class ComplexHeadData { ```java /** * 重复多次写入 - *

1. 创建excel对应的实体对象 参照{@link ComplexHeadData} - *

2. 使用{@link ExcelProperty}注解指定复杂的头 - *

3. 直接调用二次写入即可 + *

+ * 1. 创建excel对应的实体对象 参照{@link ComplexHeadData} + *

+ * 2. 使用{@link ExcelProperty}注解指定复杂的头 + *

+ * 3. 直接调用二次写入即可 */ @Test public void repeatedWrite() { @@ -586,10 +589,12 @@ public class ComplexHeadData { ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); // 这里注意 如果同一个sheet只要创建一次 WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); - // 第一次写入会创建头 - excelWriter.write(data(), writeSheet); - // 第二次写入会在上一次写入的最后一行后面写入 - excelWriter.write(data(), writeSheet); + // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来 + for (int i = 0; i < 5; i++) { + // 分页去数据库查询数据 这里可以去数据库查询每一页的数据 + List data = data(); + excelWriter.write(data, writeSheet); + } /// 千万别忘记finish 会帮忙关闭流 excelWriter.finish(); } @@ -1041,14 +1046,18 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja 参照:[对象](#simpleWriteObject) 就是名称变了下 ##### 代码 ```java - /** + /** * 文件下载 - *

1. 创建excel对应的实体对象 参照{@link DownloadData} - *

2. 设置返回的 参数 - *

3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大 + *

+ * 1. 创建excel对应的实体对象 参照{@link DownloadData} + *

+ * 2. 设置返回的 参数 + *

+ * 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大 */ @GetMapping("download") public void download(HttpServletResponse response) throws IOException { + // 这里注意 有同学反应下载的文件名不对。这个时候 请别使用swagger 他会影像 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java index d96f89b..9d5e9f0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java @@ -33,6 +33,7 @@ public class WebTest { */ @GetMapping("download") public void download(HttpServletResponse response) throws IOException { + // 这里注意 有同学反应下载的文件名不对。这个时候 请别使用swagger 他会影像 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index 094d1c1..6636769 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -110,10 +110,12 @@ public class WriteTest { ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); // 这里注意 如果同一个sheet只要创建一次 WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); - // 第一次写入会创建头 - excelWriter.write(data(), writeSheet); - // 第二次写入会在上一次写入的最后一行后面写入 - excelWriter.write(data(), writeSheet); + // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来 + for (int i = 0; i < 5; i++) { + // 分页去数据库查询数据 这里可以去数据库查询每一页的数据 + List data = data(); + excelWriter.write(data, writeSheet); + } /// 千万别忘记finish 会帮忙关闭流 excelWriter.finish(); } From 1802500088c2d2ed70f6c1170be1d4de5c5d40be Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Mon, 16 Sep 2019 18:32:06 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BD=93cell=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E5=8F=AF=E8=83=BD=E4=BC=9A=E6=8A=9B=E5=87=BA=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analysis/v07/handlers/DefaultCellHandler.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index 37d12ad..8d4eb70 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -7,6 +7,8 @@ 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_TYPE_TAG; +import java.util.Deque; +import java.util.LinkedList; import java.util.Map; import java.util.TreeMap; @@ -33,8 +35,7 @@ import com.alibaba.excel.util.StringUtils; */ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder { private final AnalysisContext analysisContext; - private String currentTag; - private String currentCellIndex; + private Deque currentTagDeque = new LinkedList(); private int curCol; private Map curRowContent = new TreeMap(); private CellData currentCellData; @@ -61,11 +62,10 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder @Override public void startHandle(String name, Attributes attributes) { - currentTag = name; + currentTagDeque.push(name); // start a cell if (CELL_TAG.equals(name)) { - currentCellIndex = attributes.getValue(ExcelXmlConstants.POSITION); - curCol = PositionUtils.getCol(currentCellIndex); + curCol = PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION)); // t="s" ,it's means String // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml' @@ -100,6 +100,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder @Override public void endHandle(String name) { + currentTagDeque.pop(); if (CELL_VALUE_TAG.equals(name)) { // Have to go "sharedStrings.xml" and get it if (currentCellData.getType() == CellDataTypeEnum.STRING) { @@ -133,6 +134,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder if (StringUtils.isEmpty(currentCellValue)) { return; } + String currentTag = currentTagDeque.peek(); if (currentTag == null) { return; } From b4b610122746a995038fc4c7d0ebb4507421d8fd Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 17 Sep 2019 18:23:31 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E7=AD=89=E9=95=BF=E6=95=B0=E5=AD=97=E5=8F=AF=E8=83=BD=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E7=A7=91=E5=AD=A6=E8=AE=A1=E6=95=B0=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20#583?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- quickstart.md | 2 +- .../v03/handlers/FormulaRecordHandler.java | 4 +++- .../v03/handlers/NumberRecordHandler.java | 4 +++- .../v07/handlers/DefaultCellHandler.java | 5 ++-- .../bigdecimal/BigDecimalNumberConverter.java | 4 ++-- .../BooleanNumberConverter.java | 12 ++++------ .../byteconverter/ByteNumberConverter.java | 6 +++-- .../converters/date/DateNumberConverter.java | 16 +++++++++---- .../DoubleNumberConverter.java | 6 +++-- .../floatconverter/FloatNumberConverter.java | 6 +++-- .../integer/IntegerNumberConverter.java | 6 +++-- .../longconverter/LongNumberConverter.java | 6 +++-- .../shortconverter/ShortNumberConverter.java | 6 +++-- .../string/StringNumberConverter.java | 15 ++++++------ .../com/alibaba/excel/metadata/CellData.java | 24 ++++++++++--------- .../alibaba/excel/write/ExcelBuilderImpl.java | 2 +- .../LongestMatchColumnWidthStyleStrategy.java | 2 +- .../easyexcel/test/temp/simple/HgTest.java | 2 +- update.md | 5 ++++ 20 files changed, 83 insertions(+), 52 deletions(-) diff --git a/pom.xml b/pom.xml index 0a62923..1e5785d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.0.0-beta6 + 2.0.0 jar easyexcel diff --git a/quickstart.md b/quickstart.md index fccc2a8..2b6323f 100644 --- a/quickstart.md +++ b/quickstart.md @@ -8,7 +8,7 @@ * 读取图片 * 宏 #### 关于版本兼容 -目前poi用的 4.0.1 建议检查是否该版本。如果出现找不到class之类的,八成就是这个原因。 +目前poi用的 4.0.1 建议检查是否该版本。如果看到`NoClassDefFoundError`或者`ClassNotFoundException`,请查看poi相关版本是否都为4.0.1。 #### 详细参数介绍 有些参数不知道怎么用,或者有些功能不知道用什么参数,参照:[详细参数介绍](/docs/API.md) #### 开源项目不容易,如果觉得本项目对您的工作还是有帮助的话,请在右上角帮忙点个★Star。 diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java index f8899c0..0ff8f99 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java @@ -1,5 +1,7 @@ package com.alibaba.excel.analysis.v03.handlers; +import java.math.BigDecimal; + import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; import org.apache.poi.hssf.model.HSSFFormulaParser; import org.apache.poi.hssf.record.FormulaRecord; @@ -66,7 +68,7 @@ public class FormulaRecordHandler extends AbstractXlsRecordHandler { tempCellData.setFormulaValue(formulaValue); break; case NUMERIC: - this.cellData = new CellData(frec.getValue()); + this.cellData = new CellData(BigDecimal.valueOf(frec.getValue())); this.cellData.setFormula(Boolean.TRUE); this.cellData.setFormulaValue(formulaValue); break; diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java index 23aff5a..75c3128 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java @@ -1,5 +1,7 @@ package com.alibaba.excel.analysis.v03.handlers; +import java.math.BigDecimal; + import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.Record; @@ -29,7 +31,7 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler { NumberRecord numrec = (NumberRecord)record; this.row = numrec.getRow(); 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.setDataFormatString(formatListener.getFormatString(numrec)); } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index 8d4eb70..c0ad706 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -7,6 +7,7 @@ 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_TYPE_TAG; +import java.math.BigDecimal; import java.util.Deque; import java.util.LinkedList; import java.util.Map; @@ -164,8 +165,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder case NUMBER: case EMPTY: currentCellData.setType(CellDataTypeEnum.NUMBER); - if (currentCellData.getDoubleValue() == null) { - currentCellData.setDoubleValue(Double.valueOf(currentCellValue)); + if (currentCellData.getNumberValue() == null) { + currentCellData.setNumberValue(new BigDecimal(currentCellValue)); } break; default: diff --git a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java index f7f1405..bf9b735 100644 --- a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java @@ -28,12 +28,12 @@ public class BigDecimalNumberConverter implements Converter { @Override public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return BigDecimal.valueOf(cellData.getDoubleValue()); + return cellData.getNumberValue(); } @Override public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(value.doubleValue()); + return new CellData(value); } } diff --git a/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java index 44cbdb9..06ac0c7 100644 --- a/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.booleanconverter; +import java.math.BigDecimal; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -12,10 +14,6 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty; * @author Jiaju Zhuang */ public class BooleanNumberConverter implements Converter { - - private static final Double ONE = 1.0; - private static final Double ZERO = 0.0; - @Override public Class supportJavaTypeKey() { return Boolean.class; @@ -29,7 +27,7 @@ public class BooleanNumberConverter implements Converter { @Override public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - if (ONE.equals(cellData.getDoubleValue())) { + if (BigDecimal.ONE.equals(cellData.getNumberValue())) { return Boolean.TRUE; } return Boolean.FALSE; @@ -39,9 +37,9 @@ public class BooleanNumberConverter implements Converter { public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { if (value) { - return new CellData(ONE); + return new CellData(BigDecimal.ONE); } - return new CellData(ZERO); + return new CellData(BigDecimal.ZERO); } } diff --git a/src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java index c3ecf47..8c16a77 100644 --- a/src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.byteconverter; +import java.math.BigDecimal; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,13 +28,13 @@ public class ByteNumberConverter implements Converter { @Override public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return cellData.getDoubleValue().byteValue(); + return cellData.getNumberValue().byteValue(); } @Override public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(value.doubleValue()); + return new CellData(BigDecimal.valueOf(value)); } } diff --git a/src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java b/src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java index 810188a..fcc76bb 100644 --- a/src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java @@ -1,8 +1,9 @@ package com.alibaba.excel.converters.date; +import java.math.BigDecimal; 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.enums.CellDataTypeEnum; @@ -31,9 +32,10 @@ public class DateNumberConverter implements Converter { public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { - return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), globalConfiguration.getUse1904windowing(), null); + return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(), + globalConfiguration.getUse1904windowing(), null); } else { - return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), + return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(), contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null); } } @@ -41,6 +43,12 @@ public class DateNumberConverter implements Converter { @Override public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty, 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()))); + } } } diff --git a/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java b/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java index f272c82..15fbdae 100644 --- a/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.doubleconverter; +import java.math.BigDecimal; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,13 +28,13 @@ public class DoubleNumberConverter implements Converter { @Override public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return cellData.getDoubleValue(); + return cellData.getNumberValue().doubleValue(); } @Override public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(value); + return new CellData(BigDecimal.valueOf(value)); } } diff --git a/src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java b/src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java index 8913a57..a1b6471 100644 --- a/src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.floatconverter; +import java.math.BigDecimal; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,13 +28,13 @@ public class FloatNumberConverter implements Converter { @Override public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return cellData.getDoubleValue().floatValue(); + return cellData.getNumberValue().floatValue(); } @Override public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(value.doubleValue()); + return new CellData(BigDecimal.valueOf(value)); } } diff --git a/src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java b/src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java index 4dff32d..3b0deac 100644 --- a/src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.integer; +import java.math.BigDecimal; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,13 +28,13 @@ public class IntegerNumberConverter implements Converter { @Override public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return cellData.getDoubleValue().intValue(); + return cellData.getNumberValue().intValue(); } @Override public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(value.doubleValue()); + return new CellData(BigDecimal.valueOf(value)); } } diff --git a/src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java b/src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java index bceedd5..8058d62 100644 --- a/src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.longconverter; +import java.math.BigDecimal; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,13 +28,13 @@ public class LongNumberConverter implements Converter { @Override public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return cellData.getDoubleValue().longValue(); + return cellData.getNumberValue().longValue(); } @Override public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(value.doubleValue()); + return new CellData(BigDecimal.valueOf(value)); } } diff --git a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java index 91f6579..7d1d7da 100644 --- a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.shortconverter; +import java.math.BigDecimal; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,13 +28,13 @@ public class ShortNumberConverter implements Converter { @Override public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return cellData.getDoubleValue().shortValue(); + return cellData.getNumberValue().shortValue(); } @Override public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(value.doubleValue()); + return new CellData(BigDecimal.valueOf(value)); } } diff --git a/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java b/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java index 62b2523..f536a08 100644 --- a/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java @@ -1,6 +1,7 @@ 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 com.alibaba.excel.converters.Converter; @@ -34,30 +35,30 @@ public class StringNumberConverter implements Converter { // If there are "DateTimeFormat", read as date if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) { return DateUtils.format( - HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), + DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(), contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null), contentProperty.getDateTimeFormatProperty().getFormat()); } // If there are "NumberFormat", read as number if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) { - return NumberUtils.format(cellData.getDoubleValue(), contentProperty); + return NumberUtils.format(cellData.getNumberValue(), contentProperty); } // Excel defines formatting if (cellData.getDataFormat() != null) { 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)); } else { - return NumberUtils.format(cellData.getDoubleValue(), contentProperty); + return NumberUtils.format(cellData.getNumberValue(), contentProperty); } } // Default conversion number - return NumberUtils.format(cellData.getDoubleValue(), contentProperty); + return NumberUtils.format(cellData.getNumberValue(), contentProperty); } @Override public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new CellData(Double.valueOf(value)); + return new CellData(new BigDecimal(value)); } } diff --git a/src/main/java/com/alibaba/excel/metadata/CellData.java b/src/main/java/com/alibaba/excel/metadata/CellData.java index 0a1ffeb..7eb8de6 100644 --- a/src/main/java/com/alibaba/excel/metadata/CellData.java +++ b/src/main/java/com/alibaba/excel/metadata/CellData.java @@ -1,5 +1,7 @@ package com.alibaba.excel.metadata; +import java.math.BigDecimal; + import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.util.StringUtils; @@ -13,7 +15,7 @@ public class CellData { /** * {@link CellDataTypeEnum#NUMBER} */ - private Double doubleValue; + private BigDecimal numberValue; /** * {@link CellDataTypeEnum#STRING} and{@link CellDataTypeEnum#ERROR} */ @@ -36,7 +38,7 @@ public class CellData { public CellData(CellData other) { this.type = other.type; - this.doubleValue = other.doubleValue; + this.numberValue = other.numberValue; this.stringValue = other.stringValue; this.booleanValue = other.booleanValue; this.formula = other.formula; @@ -62,12 +64,12 @@ public class CellData { this.formula = Boolean.FALSE; } - public CellData(Double doubleValue) { - if (doubleValue == null) { + public CellData(BigDecimal numberValue) { + if (numberValue == null) { throw new IllegalArgumentException("DoubleValue can not be null"); } this.type = CellDataTypeEnum.NUMBER; - this.doubleValue = doubleValue; + this.numberValue = numberValue; this.formula = Boolean.FALSE; } @@ -105,12 +107,12 @@ public class CellData { this.type = type; } - public Double getDoubleValue() { - return doubleValue; + public BigDecimal getNumberValue() { + return numberValue; } - public void setDoubleValue(Double doubleValue) { - this.doubleValue = doubleValue; + public void setNumberValue(BigDecimal numberValue) { + this.numberValue = numberValue; } public String getStringValue() { @@ -181,7 +183,7 @@ public class CellData { } return; case NUMBER: - if (doubleValue == null) { + if (numberValue == null) { type = CellDataTypeEnum.EMPTY; } return; @@ -198,7 +200,7 @@ public class CellData { public String toString() { switch (type) { case NUMBER: - return doubleValue.toString(); + return numberValue.toString(); case BOOLEAN: return booleanValue.toString(); case STRING: diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 3c25726..6f0ae1c 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -312,7 +312,7 @@ public class ExcelBuilderImpl implements ExcelBuilder { cell.setCellValue(cellData.getBooleanValue()); return cellData; case NUMBER: - cell.setCellValue(cellData.getDoubleValue()); + cell.setCellValue(cellData.getNumberValue().doubleValue()); return cellData; case IMAGE: setImageValue(cellData, cell); diff --git a/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java index 5c6ec8c..320573b 100644 --- a/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java @@ -58,7 +58,7 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty case BOOLEAN: return cellData.getBooleanValue().toString().getBytes().length; case NUMBER: - return cellData.getDoubleValue().toString().getBytes().length; + return cellData.getNumberValue().toString().getBytes().length; default: return -1; } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java index 4ece35e..a70275b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java @@ -24,7 +24,7 @@ public class HgTest { @Test public void hh() throws IOException { List 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) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } diff --git a/update.md b/update.md index 60dd1ed..0efb5ef 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,8 @@ +# 2.0.0 +* 修复当cell为空可能会抛出空指针的bug +* 修复电话等长数字可能出现科学计数法的问题 [Issue #583](https://github.com/alibaba/easyexcel/issues/583) +* 升级为正式版 + # 2.0.0-beta6 * 修复空行读取空指针异常 * 修复写入指定头为List>,但是数据用List导致的空指针 From ec569824c39948800881ecb372368769d40366bf Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 17 Sep 2019 22:50:22 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E7=AD=89=E9=95=BF=E6=95=B0=E5=AD=97=E5=8F=AF=E8=83=BD=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E7=A7=91=E5=AD=A6=E8=AE=A1=E6=95=B0=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20#583?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converters/booleanconverter/BooleanNumberConverter.java | 2 +- .../test/core/converter/ReadAllConverterDataListener.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java index 06ac0c7..a0042ea 100644 --- a/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java +++ b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java @@ -27,7 +27,7 @@ public class BooleanNumberConverter implements Converter { @Override public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - if (BigDecimal.ONE.equals(cellData.getNumberValue())) { + if (BigDecimal.ONE.compareTo(cellData.getNumberValue()) == 0) { return Boolean.TRUE; } return Boolean.FALSE; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java index f4f0a1e..c8b5d8b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java @@ -65,7 +65,8 @@ public class ReadAllConverterDataListener extends AnalysisEventListener