Browse Source

修改空字符串读取可能为空的bug

bugfix
Jiaju Zhuang 5 years ago
parent
commit
e0d718b0ab
  1. 14
      src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java
  2. 19
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  3. 22
      src/test/java/com/alibaba/easyexcel/test/temp/LockData.java
  4. 50
      src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java

14
src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java

@ -30,15 +30,23 @@ public class SharedStringsTableHandler extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) {
if (SI_TAG.equals(name)) {
currentData = "";
if (T_TAG.equals(name)) {
currentElementData = null;
} else if (SI_TAG.equals(name)) {
currentData = null;
}
}
@Override
public void endElement(String uri, String localName, String name) {
if (T_TAG.equals(name)) {
currentData += currentElementData;
if (currentData == null) {
currentData = currentElementData;
} else {
if (currentElementData != null) {
currentData += currentElementData;
}
}
} else if (SI_TAG.equals(name)) {
readCache.put(currentData);
}

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

@ -9,7 +9,12 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.demo.read.DemoData;
import com.alibaba.easyexcel.test.demo.read.DemoDataListener;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
@ -24,16 +29,21 @@ public class Lock2Test {
@Test
public void test() throws Exception {
File file = new File("D:\\test\\珠海2.xlsx");
File file = new File("D:\\test\\test001.xlsx");
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync();
LOGGER.info("数据:{}", list.size());
for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data));
}
LOGGER.info("文件状态:{}", file.exists());
file.delete();
Thread.sleep(500 * 1000);
}
@Test
public void simpleRead() {
// 写法1:
String fileName = "D:\\test\\珠海 (1).xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(fileName, LockData.class, new LockDataListener()).sheet().doRead();
}
@Test
@ -50,5 +60,4 @@ public class Lock2Test {
Thread.sleep(500 * 1000);
}
}

22
src/test/java/com/alibaba/easyexcel/test/temp/LockData.java

@ -0,0 +1,22 @@
package com.alibaba.easyexcel.test.temp;
import lombok.Data;
/**
* 基础数据类.这里的排序和excel里面的排序一致
*
* @author Jiaju Zhuang
**/
@Data
public class LockData {
private String string0;
private String string1;
private String string2;
private String string3;
private String string4;
private String string5;
private String string6;
private String string7;
private String string8;
}

50
src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java

@ -0,0 +1,50 @@
package com.alibaba.easyexcel.test.temp;
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.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
/**
* 模板的读取类
*
* @author Jiaju Zhuang
*/
public class LockDataListener 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("存储数据库成功!");
}
}
Loading…
Cancel
Save