Browse Source

修改在传入List<List<Object>>判断行数错误 #526

bugfix
Jiaju Zhuang 5 years ago
parent
commit
2b219d40d5
  1. 3
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  2. 2
      src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java
  3. 51
      src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java
  4. 39
      src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java
  5. 51
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  6. 3
      update.md

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

@ -174,7 +174,8 @@ public class ExcelBuilderImpl implements ExcelBuilder {
if (cellIndex != 0) { if (cellIndex != 0) {
cellIndex++; cellIndex++;
} }
for (int i = 0; i < oneRowData.size() - dataIndex; i++) { int size = oneRowData.size() - dataIndex;
for (int i = 0; i < size; i++) {
doAddBasicTypeToExcel(oneRowData, null, row, relativeRowIndex, dataIndex++, cellIndex++); doAddBasicTypeToExcel(oneRowData, null, row, relativeRowIndex, dataIndex++, cellIndex++);
} }
} }

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

@ -23,7 +23,7 @@ public class HgTest {
@Test @Test
public void hh() throws IOException { public void hh() throws IOException {
List<Object> list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xlsx")).autoTrim(Boolean.FALSE).sheet() List<Object> list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls")).autoTrim(Boolean.FALSE).sheet(2)
.headRowNumber(0).doReadSync(); .headRowNumber(0).doReadSync();
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));

51
src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java

@ -0,0 +1,51 @@
package com.alibaba.easyexcel.test.temp.simple;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.demo.read.DemoDataListener;
import com.alibaba.easyexcel.test.temp.LockData;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
/**
* 模板的读取类
*
* @author Jiaju Zhuang
*/
public class RepeatListener extends AnalysisEventListener<LockData> {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
/**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<LockData> list = new ArrayList<LockData>();
@Override
public void invoke(LockData data, AnalysisContext context) {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
list.add(data);
if (list.size() >= BATCH_COUNT) {
saveData();
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
saveData();
LOGGER.info("所有数据解析完成!");
}
/**
* 加上存储数据库
*/
private void saveData() {
LOGGER.info("{}条数据,开始存储数据库!", list.size());
LOGGER.info("存储数据库成功!");
}
}

39
src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java

@ -0,0 +1,39 @@
package com.alibaba.easyexcel.test.temp.simple;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.temp.LockData;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.fastjson.JSON;
/**
* 测试poi
*
* @author Jiaju Zhuang
**/
@Ignore
public class RepeatTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RepeatTest.class);
@Test
public void hh() throws IOException {
ExcelReader reader =
EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls"), LockData.class, new RepeatListener())
.headRowNumber(0).build();
ReadSheet r1 = EasyExcel.readSheet(0).build();
ReadSheet r2 = EasyExcel.readSheet(1).build();
reader.read(r1);
reader.read(r2);
reader.finish();
}
}

51
src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java

@ -0,0 +1,51 @@
package com.alibaba.easyexcel.test.temp.simple;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
/**
* 测试poi
*
* @author Jiaju Zhuang
**/
@Ignore
public class Wirte {
private static final Logger LOGGER = LoggerFactory.getLogger(Wirte.class);
@Test
public void simpleWrite() {
// 写法1
String fileName = TestFileUtil.getPath() + "ttttttttt11" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName).sheet("模板").doWrite(data());
}
private List<List<Object>> data() {
List<List<Object>> list = new ArrayList<List<Object>>();
for (int i = 0; i < 10; i++) {
List<Object> list1 = new ArrayList<Object>();
list1.add("字符串" + i);
list1.add(new Date());
list1.add(0.56);
list.add(list1);
}
return list;
}
}

3
update.md

@ -1,3 +1,6 @@
# 2.0.0-beta4
* 修改在传入List<List<Object>>判断行数错误 [Issue #526](https://github.com/alibaba/easyexcel/issues/526)
# 2.0.0-beta3 # 2.0.0-beta3
* 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386) * 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386)
* 新增读取返回头数据 * 新增读取返回头数据

Loading…
Cancel
Save