Jiaju Zhuang
5 years ago
committed by
GitHub
25 changed files with 408 additions and 15 deletions
@ -0,0 +1,63 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
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.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; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Ignore |
||||
public class Lock2Test { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Lock2Test.class); |
||||
|
||||
@Test |
||||
public void test() throws Exception { |
||||
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)); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void simpleRead() { |
||||
// 写法1:
|
||||
String fileName = "D:\\test\\珠海 (1).xlsx"; |
||||
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
|
||||
EasyExcel.read(fileName, LockData.class, new LockDataListener()).sheet().doRead(); |
||||
} |
||||
|
||||
@Test |
||||
public void test2() throws Exception { |
||||
File file = new File("D:\\test\\converter03.xls"); |
||||
|
||||
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); |
||||
} |
||||
|
||||
} |
@ -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; |
||||
|
||||
} |
@ -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("存储数据库成功!"); |
||||
} |
||||
} |
@ -0,0 +1,50 @@
|
||||
package com.alibaba.easyexcel.test.temp.poi; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
|
||||
import org.apache.poi.xssf.streaming.SXSSFRow; |
||||
import org.apache.poi.xssf.streaming.SXSSFSheet; |
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
||||
import org.apache.poi.xssf.usermodel.XSSFRow; |
||||
import org.apache.poi.xssf.usermodel.XSSFSheet; |
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
|
||||
/** |
||||
* 测试poi |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Ignore |
||||
public class Poi2Test { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Poi2Test.class); |
||||
|
||||
@Test |
||||
public void test() throws IOException { |
||||
String file = "D:\\test\\珠海.xlsx"; |
||||
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file)); |
||||
SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); |
||||
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); |
||||
SXSSFRow row = xssfSheet.getRow(0); |
||||
LOGGER.info("第一行数据:{}", row); |
||||
} |
||||
|
||||
@Test |
||||
public void lastRowNumXSSF() throws IOException { |
||||
String file = "D:\\test\\珠海.xlsx"; |
||||
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file); |
||||
LOGGER.info("一共:{}个sheet", xssfWorkbook.getNumberOfSheets()); |
||||
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); |
||||
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); |
||||
XSSFRow row = xssfSheet.getRow(0); |
||||
LOGGER.info("第一行数据:{}", row); |
||||
xssfSheet.createRow(20); |
||||
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); |
||||
} |
||||
} |
@ -0,0 +1,33 @@
|
||||
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.excel.EasyExcel; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 测试poi |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Ignore |
||||
public class HgTest { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HgTest.class); |
||||
|
||||
@Test |
||||
public void hh() throws IOException { |
||||
List<Object> list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xlsx")).autoTrim(Boolean.FALSE).sheet() |
||||
.headRowNumber(0).doReadSync(); |
||||
for (Object data : list) { |
||||
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue