Search in sources :

Example 66 with ExcelWriter

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());
    }
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) SyncReadListener(com.alibaba.excel.event.SyncReadListener) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) SimpleData(com.alibaba.easyexcel.test.core.simple.SimpleData) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 67 with ExcelWriter

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);
}
Also used : SXSSFRow(org.apache.poi.xssf.streaming.SXSSFRow) ExcelWriter(com.alibaba.excel.ExcelWriter) FileOutputStream(java.io.FileOutputStream) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) SXSSFCell(org.apache.poi.xssf.streaming.SXSSFCell) SXSSFSheet(org.apache.poi.xssf.streaming.SXSSFSheet) Test(org.junit.Test)

Aggregations

ExcelWriter (com.alibaba.excel.ExcelWriter)67 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)52 Test (org.junit.Test)31 Sheet (com.alibaba.excel.metadata.Sheet)13 HashMap (java.util.HashMap)13 FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)11 List (java.util.List)9 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)7 WriteTable (com.alibaba.excel.write.metadata.WriteTable)6 ArrayList (java.util.ArrayList)6 ExcelReader (com.alibaba.excel.ExcelReader)5 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)5 FillWrapper (com.alibaba.excel.write.metadata.fill.FillWrapper)4 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)4 OutputStream (java.io.OutputStream)4 Map (java.util.Map)4 LargeDataTest (com.alibaba.easyexcel.test.core.large.LargeDataTest)3 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)3 WriteExcelException (com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)3