-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77a40d8
commit 2b219d4
Showing
6 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("存储数据库成功!"); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters