Search in sources :

Example 1 with ReadSheet

use of com.alibaba.excel.read.metadata.ReadSheet in project easyexcel by alibaba.

the class RepeatTest method hh.

@Test
public void hh() throws IOException {
    ExcelReader reader = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls"), LockData.class, new RepeatListener()).headRowNumber(0).build();
    ReadSheet r1 = EasyExcel.readSheet(0).build();
    ReadSheet r2 = EasyExcel.readSheet(2).build();
    reader.read(r1);
    reader.read(r2);
    reader.finish();
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with ReadSheet

use of com.alibaba.excel.read.metadata.ReadSheet in project easyexcel by alibaba.

the class BofRecordHandler method processRecord.

@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    BOFRecord br = (BOFRecord) record;
    XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder();
    if (br.getType() == BOFRecord.TYPE_WORKBOOK) {
        xlsReadWorkbookHolder.setReadSheetIndex(null);
        xlsReadWorkbookHolder.setIgnoreRecord(Boolean.FALSE);
        return;
    }
    if (br.getType() != BOFRecord.TYPE_WORKSHEET) {
        return;
    }
    // Init read sheet Data
    initReadSheetDataList(xlsReadWorkbookHolder);
    Integer readSheetIndex = xlsReadWorkbookHolder.getReadSheetIndex();
    if (readSheetIndex == null) {
        readSheetIndex = 0;
        xlsReadWorkbookHolder.setReadSheetIndex(readSheetIndex);
    }
    ReadSheet actualReadSheet = xlsReadWorkbookHolder.getActualSheetDataList().get(readSheetIndex);
    assert actualReadSheet != null : "Can't find the sheet.";
    // Copy the parameter to the current sheet
    ReadSheet readSheet = SheetUtils.match(actualReadSheet, xlsReadContext);
    if (readSheet != null) {
        xlsReadContext.currentSheet(readSheet);
        xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.FALSE);
    } else {
        xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.TRUE);
    }
    // Go read the next one
    xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1);
}
Also used : XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) BOFRecord(org.apache.poi.hssf.record.BOFRecord) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 3 with ReadSheet

use of com.alibaba.excel.read.metadata.ReadSheet in project easyexcel by alibaba.

the class MultipleSheetsDataTest method read.

private void read(File file) {
    MultipleSheetsListener multipleSheetsListener = new MultipleSheetsListener();
    try (ExcelReader excelReader = EasyExcel.read(file, MultipleSheetsData.class, multipleSheetsListener).build()) {
        List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
        int count = 1;
        for (ReadSheet readSheet : sheets) {
            excelReader.read(readSheet);
            Assert.assertEquals(multipleSheetsListener.getList().size(), count);
            count++;
        }
    }
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 4 with ReadSheet

use of com.alibaba.excel.read.metadata.ReadSheet in project easyexcel by alibaba.

the class RepetitionDataTest method readAndWrite.

private void readAndWrite(File file) {
    try (ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build()) {
        WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
        excelWriter.write(data(), writeSheet).write(data(), writeSheet);
    }
    try (ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build()) {
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
    }
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 5 with ReadSheet

use of com.alibaba.excel.read.metadata.ReadSheet in project easyexcel by alibaba.

the class RepetitionDataTest method readAndWriteTable.

private void readAndWriteTable(File file) {
    try (ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build()) {
        WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
        WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).build();
        excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable);
    }
    try (ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build()) {
        ReadSheet readSheet = EasyExcel.readSheet(0).headRowNumber(2).build();
        excelReader.read(readSheet);
    }
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Aggregations

ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)20 ExcelReader (com.alibaba.excel.ExcelReader)15 Test (org.junit.Test)6 ExcelWriter (com.alibaba.excel.ExcelWriter)5 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)5 WriteTable (com.alibaba.excel.write.metadata.WriteTable)3 FileInputStream (java.io.FileInputStream)3 ArrayList (java.util.ArrayList)3 MapCache (com.alibaba.excel.cache.MapCache)2 AnalysisContext (com.alibaba.excel.context.AnalysisContext)2 FileOutputStream (java.io.FileOutputStream)2 SimpleData (com.alibaba.easyexcel.test.core.simple.SimpleData)1 TestFileUtil (com.alibaba.easyexcel.test.util.TestFileUtil)1 EasyExcel (com.alibaba.excel.EasyExcel)1 XlsxRowHandler (com.alibaba.excel.analysis.v07.handlers.sax.XlsxRowHandler)1 ExcelProperty (com.alibaba.excel.annotation.ExcelProperty)1 DateTimeFormat (com.alibaba.excel.annotation.format.DateTimeFormat)1 NumberFormat (com.alibaba.excel.annotation.format.NumberFormat)1 DefaultConverterLoader (com.alibaba.excel.converters.DefaultConverterLoader)1 StringStringConverter (com.alibaba.excel.converters.string.StringStringConverter)1