Browse Source

新增没有对象的demo

developing
Jiaju Zhuang 5 years ago
parent
commit
b675ba50c5
  1. 50
      src/test/java/com/alibaba/easyexcel/test/demo/read/NoModleDataListener.java
  2. 9
      src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
  3. 24
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

50
src/test/java/com/alibaba/easyexcel/test/demo/read/NoModleDataListener.java

@ -0,0 +1,50 @@
package com.alibaba.easyexcel.test.demo.read;
import java.util.ArrayList;
import java.util.List;
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;
/**
* 直接用map接收数据
*
* @author Jiaju Zhuang
*/
public class NoModleDataListener extends AnalysisEventListener<Map<Integer, String>> {
private static final Logger LOGGER = LoggerFactory.getLogger(NoModleDataListener.class);
/**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
@Override
public void invoke(Map<Integer, String> 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("存储数据库成功!");
}
}

9
src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java

@ -217,4 +217,13 @@ public class ReadTest {
} }
} }
/**
* 不创建对象的读不是特别推荐使用都用String接收对日期的支持不是很好
*/
@Test
public void noModleRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 只要,然后读取第一个sheet 同步读取会自动finish
EasyExcel.read(fileName, new NoModleDataListener()).sheet().doRead();
}
} }

24
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -58,7 +58,6 @@ public class WriteTest {
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet); excelWriter.write(data(), writeSheet);
// 进行加密
/// 千万别忘记finish 会帮忙关闭流 /// 千万别忘记finish 会帮忙关闭流
excelWriter.finish(); excelWriter.finish();
} }
@ -379,6 +378,17 @@ public class WriteTest {
.registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data()); .registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data());
} }
/**
* 不创建对象的写
*/
@Test
public void noModleWrite() {
// 写法1
String fileName = TestFileUtil.getPath() + "noModleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList());
}
private List<LongestMatchColumnWidthData> dataLong() { private List<LongestMatchColumnWidthData> dataLong() {
List<LongestMatchColumnWidthData> list = new ArrayList<LongestMatchColumnWidthData>(); List<LongestMatchColumnWidthData> list = new ArrayList<LongestMatchColumnWidthData>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
@ -405,6 +415,18 @@ public class WriteTest {
return list; return list;
} }
private List<List<Object>> dataList() {
List<List<Object>> list = new ArrayList<List<Object>>();
for (int i = 0; i < 10; i++) {
List<Object> data = new ArrayList<Object>();
data.add("字符串" + i);
data.add(new Date());
data.add(0.56);
list.add(data);
}
return list;
}
private List<DemoData> data() { private List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>(); List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {

Loading…
Cancel
Save