From 3ea045c502a639d24bbc5874ad8761578d911874 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 19 Sep 2019 20:53:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E3=80=81=E5=AF=BC=E5=87=BA=E6=94=AF=E6=8C=81=E5=85=AC?= =?UTF-8?q?=E5=BC=8F=20=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=8D=95=E5=85=83=E6=A0=BC=E7=B1=BB=E5=9E=8B=E3=80=81?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=8C=87=E5=AE=9A=E5=8D=95=E5=85=83=E6=A0=BC?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../alibaba/excel/util/ConverterUtils.java | 2 +- .../alibaba/excel/write/ExcelBuilderImpl.java | 2 + .../test/core/celldata/CellDataData.java | 21 ++++++ .../core/celldata/CellDataDataListener.java | 37 ++++++++++ .../test/core/celldata/CellDataDataTest.java | 71 +++++++++++++++++++ update.md | 15 ++++ 7 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java diff --git a/pom.xml b/pom.xml index 754c945..68fe674 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.0.0 + 2.1.0-beta1 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/util/ConverterUtils.java b/src/main/java/com/alibaba/excel/util/ConverterUtils.java index a06ab38..1a1e70a 100644 --- a/src/main/java/com/alibaba/excel/util/ConverterUtils.java +++ b/src/main/java/com/alibaba/excel/util/ConverterUtils.java @@ -78,7 +78,7 @@ public class ConverterUtils { Class classGeneric; if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType)type; - classGeneric = (Class)((ParameterizedType)parameterizedType.getActualTypeArguments()[0]).getRawType(); + classGeneric = (Class)parameterizedType.getActualTypeArguments()[0]; } else { classGeneric = String.class; } diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index e0e8503..5a21d44 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -29,8 +29,10 @@ import com.alibaba.excel.metadata.BaseRowModel; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.FileUtils; +import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java new file mode 100644 index 0000000..a4e624f --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java @@ -0,0 +1,21 @@ +package com.alibaba.easyexcel.test.core.celldata; + +import java.util.Date; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.format.DateTimeFormat; +import com.alibaba.excel.metadata.CellData; + +import lombok.Data; + +/** + * @author Jiaju Zhuang + */ +@Data +public class CellDataData { + @DateTimeFormat("yyyy年MM月dd日") + private CellData date; + private CellData integer1; + private Integer integer2; + private CellData formulaValue; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java new file mode 100644 index 0000000..3108f97 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java @@ -0,0 +1,37 @@ +package com.alibaba.easyexcel.test.core.celldata; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.fastjson.JSON; + +/** + * @author Jiaju Zhuang + */ +public class CellDataDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDataListener.class); + List list = new ArrayList(); + + @Override + public void invoke(CellDataData data, AnalysisContext context) { + list.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assert.assertEquals(list.size(), 1); + CellDataData cellDataData = list.get(0); + + Assert.assertEquals(cellDataData.getDate().getStringValue(), "2020年01月01日"); + Assert.assertEquals((long)cellDataData.getInteger1().getData(), 2L); + Assert.assertEquals((long)cellDataData.getInteger2(), 2L); + Assert.assertEquals(cellDataData.getFormulaValue().getFormulaValue(), "B2+C2"); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java new file mode 100644 index 0000000..4615cdb --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java @@ -0,0 +1,71 @@ +package com.alibaba.easyexcel.test.core.celldata; + +import java.io.File; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.junit.Assert; +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.SimpleDataListener; +import com.alibaba.easyexcel.test.core.simple.SimpleDataSheetNameListener; +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.util.DateUtils; + +/** + * + * @author Jiaju Zhuang + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class CellDataDataTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("cellData07.xlsx"); + file03 = TestFileUtil.createNewFile("cellData03.xls"); + } + + @Test + public void t01ReadAndWrite07() throws Exception { + readAndWrite(file07); + } + + @Test + public void t02ReadAndWrite03() throws Exception { + readAndWrite(file03); + } + + private void readAndWrite(File file) throws Exception { + EasyExcel.write(file, CellDataData.class).sheet().doWrite(data()); + EasyExcel.read(file, CellDataData.class, new CellDataDataListener()).sheet().doRead(); + } + + private List data() throws Exception { + List list = new ArrayList(); + CellDataData cellDataData = new CellDataData(); + cellDataData.setDate(new CellData(DateUtils.parseDate("2020-01-01 01:01:01"))); + CellData integer1 = new CellData(); + integer1.setType(CellDataTypeEnum.NUMBER); + integer1.setNumberValue(BigDecimal.valueOf(2L)); + cellDataData.setInteger1(integer1); + cellDataData.setInteger2(2); + CellData formulaValue = new CellData(); + formulaValue.setFormula(Boolean.TRUE); + formulaValue.setFormulaValue("B2+C2"); + cellDataData.setFormulaValue(formulaValue); + list.add(cellDataData); + return list; + } +} diff --git a/update.md b/update.md index 0efb5ef..47cb445 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,8 @@ +# 2.1.0-beta1 +* 降级poi为3.1.7 兼容jdk6 +* 新增支持导入、导出支持公式 +* 新增支持读取单元格类型、写入指定单元格类型 + # 2.0.0 * 修复当cell为空可能会抛出空指针的bug * 修复电话等长数字可能出现科学计数法的问题 [Issue #583](https://github.com/alibaba/easyexcel/issues/583) @@ -33,23 +38,33 @@ * sheetNo 改成0开始 * 读支持指定列名 * 升级poi 到4.0.1 + # 1.2.4 修复read()方法存在的bug + # 1.2.1 修复POI在大并发情况下创建临时目录失败的bug + # 1.0.9 修复excel超过16列被覆盖的问题,修复数据只有一行时候无法透传的bug。 + # 1.0.8 如果整行excel数据全部为空,则不解析返回。完善多sheet的解析。 + # 1.0.6 增加@ExcelColumnNum,修复字符串前后空白,增加过滤功能。 + # 1.0.5 优化类型转换的性能。 + # 1.0.4 修复日期类型转换时候数字问题。基础模型支持字段类型int,long,double,boolean,date,string + # 1.0.3 修复无@ExcelProperty标注的多余字段时候报错。 + # 1.0.2 修复拿到一行数据后,存到list中,但最后处理时候变为空的bug。 + # 1.0.1 完善测试用例,防止歧义,模型字段映射不上时候有抛异常,改为提醒。 \ No newline at end of file