diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java index 61f38733..329b2245 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java @@ -25,15 +25,13 @@ public class RowTagHandler extends AbstractXlsxTagHandler { int rowIndex = PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_R), xlsxReadSheetHolder.getRowIndex()); Integer lastRowIndex = xlsxReadContext.readSheetHolder().getRowIndex(); - if (lastRowIndex != null) { - while (lastRowIndex + 1 < rowIndex) { - xlsxReadContext.readRowHolder(new ReadRowHolder(lastRowIndex + 1, RowTypeEnum.EMPTY, - xlsxReadSheetHolder.getGlobalConfiguration(), new LinkedHashMap())); - xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext); - xlsxReadSheetHolder.setColumnIndex(null); - xlsxReadSheetHolder.setCellMap(new LinkedHashMap()); - lastRowIndex++; - } + while (lastRowIndex + 1 < rowIndex) { + xlsxReadContext.readRowHolder(new ReadRowHolder(lastRowIndex + 1, RowTypeEnum.EMPTY, + xlsxReadSheetHolder.getGlobalConfiguration(), new LinkedHashMap())); + xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext); + xlsxReadSheetHolder.setColumnIndex(null); + xlsxReadSheetHolder.setCellMap(new LinkedHashMap()); + lastRowIndex++; } xlsxReadSheetHolder.setRowIndex(rowIndex); } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java index 87185dc2..7a5272a6 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java @@ -15,6 +15,7 @@ import com.alibaba.excel.read.metadata.ReadSheet; * @author Jiaju Zhuang */ public class ReadSheetHolder extends AbstractReadHolder { + /** * current param */ @@ -59,6 +60,7 @@ public class ReadSheetHolder extends AbstractReadHolder { this.sheetNo = readSheet.getSheetNo(); this.sheetName = readSheet.getSheetName(); this.cellMap = new LinkedHashMap(); + this.rowIndex = -1; } public ReadSheet getReadSheet() { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java new file mode 100644 index 00000000..01fbb466 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java @@ -0,0 +1,42 @@ +package com.alibaba.easyexcel.test.temp.read; + +import java.util.Map; + +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 HeadListener extends AnalysisEventListener> { + private static final Logger LOGGER = LoggerFactory.getLogger(HeadListener.class); + /** + * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 5; + + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + LOGGER.info("HEAD:{}", JSON.toJSONString(headMap)); + LOGGER.info("total:{}", context.readSheetHolder().getTotal()); + + } + + @Override + public void invoke(Map data, AnalysisContext context) { + LOGGER.info("index:{}", context.readRowHolder().getRowIndex()); + LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + LOGGER.info("所有数据解析完成!"); + } + +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java index b046d143..a197a3b2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java @@ -7,6 +7,7 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.cache.Ehcache; @@ -21,8 +22,8 @@ public class HeadReadTest { @Test public void test() throws Exception { - File file = new File("D:\\test\\headt1.xls"); - EasyExcel.read(file, HeadReadData.class, new HDListener()).sheet(0).doRead(); + File file = TestFileUtil.readUserHomeFile("test/t1.xlsx"); + EasyExcel.read(file, null, new HeadListener()).ignoreEmptyRow(false).sheet(0).doRead(); } diff --git a/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java b/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java index 2031e70a..d4665b44 100644 --- a/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java +++ b/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java @@ -5,6 +5,7 @@ import java.io.InputStream; public class TestFileUtil { + public static InputStream getResourcesFileInputStream(String fileName) { return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName); } @@ -29,4 +30,7 @@ public class TestFileUtil { return new File(getPath() + pathName); } + public static File readUserHomeFile(String pathName) { + return new File(System.getProperty("user.home") + File.separator + pathName); + } }