Browse Source

新增支持导入、导出支持公式 新增支持读取单元格类型、写入指定单元格类型

pull/635/head
Jiaju Zhuang 5 years ago
parent
commit
3ea045c502
  1. 2
      pom.xml
  2. 2
      src/main/java/com/alibaba/excel/util/ConverterUtils.java
  3. 2
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  4. 21
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java
  5. 37
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java
  6. 71
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java
  7. 15
      update.md

2
pom.xml

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

2
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;
}

2
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;

21
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> date;
private CellData<Integer> integer1;
private Integer integer2;
private CellData formulaValue;
}

37
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<CellDataData> {
private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDataListener.class);
List<CellDataData> list = new ArrayList<CellDataData>();
@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)));
}
}

71
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<CellDataData> data() throws Exception {
List<CellDataData> list = new ArrayList<CellDataData>();
CellDataData cellDataData = new CellDataData();
cellDataData.setDate(new CellData<Date>(DateUtils.parseDate("2020-01-01 01:01:01")));
CellData<Integer> integer1 = new CellData<Integer>();
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;
}
}

15
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
完善测试用例,防止歧义,模型字段映射不上时候有抛异常,改为提醒。
Loading…
Cancel
Save