Search in sources :

Example 21 with ExcelReader

use of com.alibaba.excel.ExcelReader in project easyexcel by alibaba.

the class ParameterDataTest method readAndWrite5.

private void readAndWrite5(File file, ExcelTypeEnum type) throws Exception {
    ExcelWriter excelWriter = EasyExcel.write(new FileOutputStream(file)).excelType(type).head(ParameterData.class).relativeHeadRowIndex(0).build();
    WriteSheet writeSheet = EasyExcel.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build();
    WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).needHead(Boolean.TRUE).build();
    excelWriter.write(data(), writeSheet, writeTable);
    excelWriter.finish();
    ExcelReader excelReader = EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
    ReadSheet readSheet = EasyExcel.readSheet().head(ParameterData.class).use1904windowing(Boolean.FALSE).headRowNumber(1).sheetNo(0).sheetName("0").build();
    excelReader.read(readSheet);
    excelReader.finish();
    excelReader = EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
    excelReader.read();
    excelReader.finish();
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) MapCache(com.alibaba.excel.cache.MapCache) ExcelWriter(com.alibaba.excel.ExcelWriter) FileOutputStream(java.io.FileOutputStream) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 22 with ExcelReader

use of com.alibaba.excel.ExcelReader in project easyexcel by alibaba.

the class ParameterDataTest method readAndWrite6.

private void readAndWrite6(File file, ExcelTypeEnum type) throws Exception {
    ExcelWriter excelWriter = EasyExcel.write(new FileOutputStream(file)).excelType(type).head(ParameterData.class).relativeHeadRowIndex(0).build();
    WriteSheet writeSheet = EasyExcel.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build();
    WriteTable writeTable = EasyExcel.writerTable(0).registerConverter(new StringStringConverter()).relativeHeadRowIndex(0).needHead(Boolean.TRUE).build();
    excelWriter.write(data(), writeSheet, writeTable);
    excelWriter.finish();
    ExcelReader excelReader = EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
    ReadSheet readSheet = EasyExcel.readSheet("0").head(ParameterData.class).use1904windowing(Boolean.FALSE).headRowNumber(1).sheetNo(0).build();
    excelReader.read(readSheet);
    excelReader.finish();
    excelReader = EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
    excelReader.read();
    excelReader.finish();
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) MapCache(com.alibaba.excel.cache.MapCache) ExcelWriter(com.alibaba.excel.ExcelWriter) FileOutputStream(java.io.FileOutputStream) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet) StringStringConverter(com.alibaba.excel.converters.string.StringStringConverter)

Example 23 with ExcelReader

use of com.alibaba.excel.ExcelReader 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 24 with ExcelReader

use of com.alibaba.excel.ExcelReader in project chunjun by DTStack.

the class ExcelFileReadClient method open.

@Override
public void open(InputStream inputStream, FtpConfig ftpConfig) throws IOException {
    cellCount = ftpConfig.getColumn().size();
    ExcelReadListener listener = new ExcelReadListener();
    queue = listener.getQueue();
    ExcelReaderBuilder builder = EasyExcel.read(inputStream, listener);
    if (!ftpConfig.getIsFirstLineHeader()) {
        builder.headRowNumber(0);
    }
    builder.ignoreEmptyRow(true);
    ExcelReader reader = builder.build();
    sheetNum = reader.excelExecutor().sheetList().size();
    executorService = new ThreadPoolExecutor(1, 1, 0, NANOSECONDS, new LinkedBlockingDeque<>(2), new BasicThreadFactory.Builder().namingPattern("excel-schedule-pool-%d").uncaughtExceptionHandler(new ExcelReaderExceptionHandler()).daemon(false).build());
    ExcelReaderExecutor executor = new ExcelReaderExecutor(reader);
    executorService.execute(executor);
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ExcelReadListener(com.dtstack.flinkx.connector.ftp.client.excel.ExcelReadListener) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ExcelReaderBuilder(com.alibaba.excel.read.builder.ExcelReaderBuilder) ExcelReaderBuilder(com.alibaba.excel.read.builder.ExcelReaderBuilder) ExcelReaderExceptionHandler(com.dtstack.flinkx.connector.ftp.client.excel.ExcelReaderExceptionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ExcelReaderExecutor(com.dtstack.flinkx.connector.ftp.client.excel.ExcelReaderExecutor)

Aggregations

ExcelReader (com.alibaba.excel.ExcelReader)24 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)15 Test (org.junit.Test)6 ExcelWriter (com.alibaba.excel.ExcelWriter)5 Sheet (com.alibaba.excel.metadata.Sheet)5 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)5 FileInputStream (java.io.FileInputStream)4 WriteTable (com.alibaba.excel.write.metadata.WriteTable)3 ArrayList (java.util.ArrayList)3 MapCache (com.alibaba.excel.cache.MapCache)2 AnalysisContext (com.alibaba.excel.context.AnalysisContext)2 SyncReadListener (com.alibaba.excel.event.SyncReadListener)2 ExcelReaderBuilder (com.alibaba.excel.read.builder.ExcelReaderBuilder)2 FileOutputStream (java.io.FileOutputStream)2 List (java.util.List)2 ModelExcelListener (cn.ruiyeclub.listener.ModelExcelListener)1 StringExcelListener (cn.ruiyeclub.listener.StringExcelListener)1 SimpleData (com.alibaba.easyexcel.test.core.simple.SimpleData)1 TestFileUtil (com.alibaba.easyexcel.test.util.TestFileUtil)1 EasyExcel (com.alibaba.excel.EasyExcel)1