Browse Source

修复不创建对象写入数据异常 [Issue #1702]

developing
Jiaju Zhuang 3 years ago
parent
commit
41d1220c01
  1. 4
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  2. 35
      src/test/java/com/alibaba/easyexcel/test/demo/Test1702/Date1702.java
  3. BIN
      src/test/java/com/alibaba/easyexcel/test/demo/Test1702/Date1702.xlsx
  4. 47
      src/test/java/com/alibaba/easyexcel/test/demo/Test1702/Issue1702.java
  5. 16
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  6. 6
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  7. 1
      update.md

4
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -92,12 +92,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
}
cellIndex = entry.getKey();
Head head = entry.getValue();
doAddBasicTypeToExcel(oneRowData, head, row, relativeRowIndex, dataIndex++, cellIndex++);
doAddBasicTypeToExcel(oneRowData, head, row, relativeRowIndex, dataIndex++, cellIndex);
}
// Finish
if (dataIndex >= oneRowData.size()) {
return;
}
// fix https://github.com/alibaba/easyexcel/issues/1702
// If there is data, it is written to the next cell
if (dataIndex != 0) {
cellIndex++;
}

35
src/test/java/com/alibaba/easyexcel/test/demo/Test1702/Date1702.java

@ -1,35 +0,0 @@
package com.alibaba.easyexcel.test.demo.Test1702;
import com.alibaba.excel.annotation.ExcelProperty;
import java.util.Date;
public class Date1702 {
@ExcelProperty(index = 0)
private String str;
@ExcelProperty(index = 1)
private Date date;
@ExcelProperty(index = 2)
private double r;
public void setStr(String str) {
this.str = str;
}
public void setDate(Date date) {
this.date = date;
}
public void setR(double r) {
this.r = r;
}
public String getStr() {
return str;
}
public Date getDate(){
return date;
}
public double getR(){
return r;
}
}

BIN
src/test/java/com/alibaba/easyexcel/test/demo/Test1702/Date1702.xlsx

Binary file not shown.

47
src/test/java/com/alibaba/easyexcel/test/demo/Test1702/Issue1702.java

@ -1,47 +0,0 @@
package com.alibaba.easyexcel.test.demo.Test1702;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Issue1702 {
@Test
public void test1702() throws FileNotFoundException {
// String fileName = TestFileUtil.getPath() + "TestIssue" + File.separator + "Date1702.xlsx";
String fileName = "src/test/java/com/alibaba/easyexcel/test/demo/Test1702/Date1702.xlsx";
EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList());
InputStream inputStream = new FileInputStream(fileName);
List<Date1702> list = EasyExcelFactory.read(inputStream).head(Date1702.class).sheet(0).doReadSync();
Assert.assertEquals("字符串",list.get(0).getStr());
}
private List<List<String>> head() {
List<List<String>> list = new ArrayList<List<String>>();
List<String> head0 = new ArrayList<String>();
head0.add("表头");
list.add(head0);
return list;
}
private List<List<Object>> dataList() {
List<List<Object>> list = new ArrayList<List<Object>>();
List<Object> data = new ArrayList<Object>();
data.add("字符串");
data.add(new Date());
data.add(0.56);
list.add(data);
return list;
}
}

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

@ -601,22 +601,6 @@ public class WriteTest {
EasyExcel.write(fileName, ConverterData.class).head(variableTitleHead()).sheet("模板").doWrite(data());
}
@Test
public void noModelWriteWithOneHead() {
// 写法1
String fileName = TestFileUtil.getPath() + "noModelSimpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName).head(noModelOneHead()).sheet("模板").doWrite(dataList());
}
private List<List<String>> noModelOneHead() {
List<List<String>> list = new ArrayList<List<String>>();
List<String> head0 = new ArrayList<String>();
head0.add("表头1");
list.add(head0);
return list;
}
/**
* 不创建对象的写
*/

6
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -95,10 +95,6 @@ public class Lock2Test {
head0.add("表头");
list.add(head0);
List<String> head1 = new ArrayList<String>();
head1.add("表头2");
list.add(head1);
return list;
}
@ -107,7 +103,7 @@ public class Lock2Test {
List<Object> data = new ArrayList<Object>();
data.add("字符串");
data.add(new Date());
//data.add(0.56);
data.add(0.56);
list.add(data);
return list;
}

1
update.md

@ -23,6 +23,7 @@
* 修复不自动行高的问题 [Issue #1869](https://github.com/alibaba/easyexcel/issues/1869)
* 新增头的非空校验 [Issue #1765](https://github.com/alibaba/easyexcel/issues/1765)
* 修复某些特殊的excel读取失败的问题 [Issue #1595](https://github.com/alibaba/easyexcel/issues/1595)
* 修复不创建对象写入数据异常 [Issue #1702](https://github.com/alibaba/easyexcel/issues/1702)
# 2.2.10

Loading…
Cancel
Save