Browse Source

修改兼容第一行为空的情况

developing
Jiaju Zhuang 5 years ago
parent
commit
2384fd19dd
  1. 16
      src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java
  2. 2
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java
  3. 42
      src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java
  4. 5
      src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java
  5. 4
      src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java

16
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<Integer, Cell>()));
xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
xlsxReadSheetHolder.setColumnIndex(null);
xlsxReadSheetHolder.setCellMap(new LinkedHashMap<Integer, Cell>());
lastRowIndex++;
}
while (lastRowIndex + 1 < rowIndex) {
xlsxReadContext.readRowHolder(new ReadRowHolder(lastRowIndex + 1, RowTypeEnum.EMPTY,
xlsxReadSheetHolder.getGlobalConfiguration(), new LinkedHashMap<Integer, Cell>()));
xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
xlsxReadSheetHolder.setColumnIndex(null);
xlsxReadSheetHolder.setCellMap(new LinkedHashMap<Integer, Cell>());
lastRowIndex++;
}
xlsxReadSheetHolder.setRowIndex(rowIndex);
}

2
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<Integer, Cell>();
this.rowIndex = -1;
}
public ReadSheet getReadSheet() {

42
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<Map<Integer,String>> {
private static final Logger LOGGER = LoggerFactory.getLogger(HeadListener.class);
/**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 5;
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
LOGGER.info("HEAD:{}", JSON.toJSONString(headMap));
LOGGER.info("total:{}", context.readSheetHolder().getTotal());
}
@Override
public void invoke(Map<Integer,String> data, AnalysisContext context) {
LOGGER.info("index:{}", context.readRowHolder().getRowIndex());
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
LOGGER.info("所有数据解析完成!");
}
}

5
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();
}

4
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);
}
}

Loading…
Cancel
Save