Search in sources :

Example 1 with SyncReadListener

use of com.alibaba.excel.event.SyncReadListener in project easyexcel by alibaba.

the class ExcelReaderSheetBuilder method doReadSync.

/**
 * Synchronous reads return results
 *
 * @return
 */
public <T> List<T> doReadSync() {
    if (excelReader == null) {
        throw new ExcelAnalysisException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
    }
    SyncReadListener syncReadListener = new SyncReadListener();
    registerReadListener(syncReadListener);
    excelReader.read(build());
    excelReader.finish();
    return (List<T>) syncReadListener.getList();
}
Also used : SyncReadListener(com.alibaba.excel.event.SyncReadListener) List(java.util.List) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 2 with SyncReadListener

use of com.alibaba.excel.event.SyncReadListener in project easyexcel by alibaba.

the class ExcelReaderBuilder method doReadAllSync.

/**
 * Synchronous reads return results
 *
 * @return
 */
public <T> List<T> doReadAllSync() {
    SyncReadListener syncReadListener = new SyncReadListener();
    registerReadListener(syncReadListener);
    try (ExcelReader excelReader = build()) {
        excelReader.readAll();
        excelReader.finish();
    }
    return (List<T>) syncReadListener.getList();
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) SyncReadListener(com.alibaba.excel.event.SyncReadListener) List(java.util.List)

Example 3 with SyncReadListener

use of com.alibaba.excel.event.SyncReadListener 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)

Aggregations

SyncReadListener (com.alibaba.excel.event.SyncReadListener)3 ExcelReader (com.alibaba.excel.ExcelReader)2 List (java.util.List)2 SimpleData (com.alibaba.easyexcel.test.core.simple.SimpleData)1 ExcelWriter (com.alibaba.excel.ExcelWriter)1 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)1 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)1 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)1