use of com.alibaba.excel.ExcelWriter in project easyexcel by alibaba.
the class SkipDataTest method readAndWrite.
private void readAndWrite(File file) {
try (ExcelWriter excelWriter = EasyExcel.write(file, SimpleData.class).build()) {
WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "第一个").build();
WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "第二个").build();
WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "第三个").build();
WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "第四个").build();
excelWriter.write(data("name1"), writeSheet0);
excelWriter.write(data("name2"), writeSheet1);
excelWriter.write(data("name3"), writeSheet2);
excelWriter.write(data("name4"), writeSheet3);
}
List<SkipData> list = EasyExcel.read(file, SkipData.class, null).sheet("第二个").doReadSync();
Assert.assertEquals(1, list.size());
Assert.assertEquals("name2", list.get(0).getName());
SyncReadListener syncReadListener = new SyncReadListener();
try (ExcelReader excelReader = EasyExcel.read(file, SkipData.class, null).registerReadListener(syncReadListener).build()) {
ReadSheet readSheet1 = EasyExcel.readSheet("第二个").build();
ReadSheet readSheet3 = EasyExcel.readSheet("第四个").build();
excelReader.read(readSheet1, readSheet3);
List<Object> syncList = syncReadListener.getList();
Assert.assertEquals(2, syncList.size());
Assert.assertEquals("name2", ((SkipData) syncList.get(0)).getName());
Assert.assertEquals("name4", ((SkipData) syncList.get(1)).getName());
}
}
use of com.alibaba.excel.ExcelWriter in project easyexcel by alibaba.
the class LargeDataTest method t04Write.
@Test
public void t04Write() throws Exception {
ExcelWriter excelWriter = EasyExcel.write(fileWriteTemp07, LargeData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 2; j++) {
excelWriter.write(data(), writeSheet);
}
excelWriter.finish();
long start = System.currentTimeMillis();
excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build();
writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 5000; j++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j);
}
excelWriter.finish();
long cost = System.currentTimeMillis() - start;
LOGGER.info("write cost:{}", cost);
start = System.currentTimeMillis();
try (FileOutputStream fileOutputStream = new FileOutputStream(fileWritePoi07)) {
SXSSFWorkbook workbook = new SXSSFWorkbook();
SXSSFSheet sheet = workbook.createSheet("sheet1");
for (int i = 0; i < 100 * 5000; i++) {
SXSSFRow row = sheet.createRow(i);
for (int j = 0; j < 25; j++) {
SXSSFCell cell = row.createCell(j);
cell.setCellValue("str-" + j + "-" + i);
}
if (i % 5000 == 0) {
LOGGER.info("{} write success.", i);
}
}
workbook.write(fileOutputStream);
workbook.dispose();
workbook.close();
}
long costPoi = System.currentTimeMillis() - start;
LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start);
LOGGER.info("{} vs {}", cost, costPoi);
Assert.assertTrue(costPoi * 2 > cost);
}
Aggregations