forked from fanruan/easyexcel
Jiaju Zhuang
5 years ago
6 changed files with 147 additions and 2 deletions
@ -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<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("存储数据库成功!"); |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
|
||||
} |
@ -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<List<Object>> data() { |
||||
List<List<Object>> list = new ArrayList<List<Object>>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
List<Object> list1 = new ArrayList<Object>(); |
||||
|
||||
list1.add("字符串" + i); |
||||
list1.add(new Date()); |
||||
list1.add(0.56); |
||||
list.add(list1); |
||||
} |
||||
return list; |
||||
} |
||||
} |
Loading…
Reference in new issue