diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index e619e370..82a4119b 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -174,7 +174,8 @@ public class ExcelBuilderImpl implements ExcelBuilder { if (cellIndex != 0) { 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++); } } 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 17e22de6..d2ebb9fa 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 @@ -23,7 +23,7 @@ public class HgTest { @Test public void hh() throws IOException { - List list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xlsx")).autoTrim(Boolean.FALSE).sheet() + List list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls")).autoTrim(Boolean.FALSE).sheet(2) .headRowNumber(0).doReadSync(); for (Object data : list) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java new file mode 100644 index 00000000..aa54b396 --- /dev/null +++ b/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 { + private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); + /** + * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 5; + List list = new ArrayList(); + + @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("存储数据库成功!"); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java new file mode 100644 index 00000000..28af426e --- /dev/null +++ b/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(); + } + +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java new file mode 100644 index 00000000..62b64b57 --- /dev/null +++ b/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> data() { + List> list = new ArrayList>(); + for (int i = 0; i < 10; i++) { + List list1 = new ArrayList(); + + list1.add("字符串" + i); + list1.add(new Date()); + list1.add(0.56); + list.add(list1); + } + return list; + } +} diff --git a/update.md b/update.md index e9e7d5e5..2b350d44 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.0.0-beta4 +* 修改在传入List>判断行数错误 [Issue #526](https://github.com/alibaba/easyexcel/issues/526) + # 2.0.0-beta3 * 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386) * 新增读取返回头数据