Search in sources :

Example 1 with ReadRowHolder

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

the class EofRecordHandler method processRecord.

@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    if (xlsReadContext.readSheetHolder() == null) {
        return;
    }
    // Sometimes tables lack the end record of the last column
    if (!xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) {
        XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
        // Forge a termination data
        xlsReadContext.readRowHolder(new ReadRowHolder(xlsReadContext.xlsReadSheetHolder().getRowIndex() + 1, xlsReadSheetHolder.getTempRowType(), xlsReadContext.readSheetHolder().getGlobalConfiguration(), xlsReadSheetHolder.getCellMap()));
        xlsReadContext.analysisEventProcessor().endRow(xlsReadContext);
        xlsReadSheetHolder.setCellMap(new LinkedHashMap<Integer, Cell>());
        xlsReadSheetHolder.setTempRowType(RowTypeEnum.EMPTY);
    }
    xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext);
}
Also used : ReadRowHolder(com.alibaba.excel.read.metadata.holder.ReadRowHolder) Cell(com.alibaba.excel.metadata.Cell) XlsReadSheetHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder)

Example 2 with ReadRowHolder

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

the class RowTagHandler method endElement.

@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    RowTypeEnum rowType = MapUtils.isEmpty(xlsxReadSheetHolder.getCellMap()) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA;
    // It's possible that all of the cells in the row are empty
    if (rowType == RowTypeEnum.DATA) {
        boolean hasData = false;
        for (Cell cell : xlsxReadSheetHolder.getCellMap().values()) {
            if (!(cell instanceof ReadCellData)) {
                hasData = true;
                break;
            }
            ReadCellData<?> readCellData = (ReadCellData<?>) cell;
            if (readCellData.getType() != CellDataTypeEnum.EMPTY) {
                hasData = true;
                break;
            }
        }
        if (!hasData) {
            rowType = RowTypeEnum.EMPTY;
        }
    }
    xlsxReadContext.readRowHolder(new ReadRowHolder(xlsxReadSheetHolder.getRowIndex(), rowType, xlsxReadSheetHolder.getGlobalConfiguration(), xlsxReadSheetHolder.getCellMap()));
    xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
    xlsxReadSheetHolder.setColumnIndex(null);
    xlsxReadSheetHolder.setCellMap(new LinkedHashMap<>());
}
Also used : ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) XlsxReadSheetHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder) ReadRowHolder(com.alibaba.excel.read.metadata.holder.ReadRowHolder) RowTypeEnum(com.alibaba.excel.enums.RowTypeEnum) Cell(com.alibaba.excel.metadata.Cell)

Example 3 with ReadRowHolder

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

the class CsvExcelReadExecutor method dealRecord.

private void dealRecord(CSVRecord record, int rowIndex) {
    Map<Integer, Cell> cellMap = new LinkedHashMap<>();
    Iterator<String> cellIterator = record.iterator();
    int columnIndex = 0;
    Boolean autoTrim = csvReadContext.currentReadHolder().globalConfiguration().getAutoTrim();
    while (cellIterator.hasNext()) {
        String cellString = cellIterator.next();
        ReadCellData<String> readCellData = new ReadCellData<>();
        readCellData.setRowIndex(rowIndex);
        readCellData.setColumnIndex(columnIndex);
        // csv is an empty string of whether <code>,,</code> is read or <code>,"",</code>
        if (StringUtils.isNotBlank(cellString)) {
            readCellData.setType(CellDataTypeEnum.STRING);
            readCellData.setStringValue(autoTrim ? cellString.trim() : cellString);
        } else {
            readCellData.setType(CellDataTypeEnum.EMPTY);
        }
        cellMap.put(columnIndex++, readCellData);
    }
    RowTypeEnum rowType = MapUtils.isEmpty(cellMap) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA;
    ReadRowHolder readRowHolder = new ReadRowHolder(rowIndex, rowType, csvReadContext.readWorkbookHolder().getGlobalConfiguration(), cellMap);
    csvReadContext.readRowHolder(readRowHolder);
    csvReadContext.csvReadSheetHolder().setCellMap(cellMap);
    csvReadContext.csvReadSheetHolder().setRowIndex(rowIndex);
    csvReadContext.analysisEventProcessor().endRow(csvReadContext);
}
Also used : ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) ReadRowHolder(com.alibaba.excel.read.metadata.holder.ReadRowHolder) RowTypeEnum(com.alibaba.excel.enums.RowTypeEnum) Cell(com.alibaba.excel.metadata.Cell) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ReadRowHolder

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

the class DummyRecordHandler method processRecord.

@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
    if (record instanceof LastCellOfRowDummyRecord) {
        // End of this row
        LastCellOfRowDummyRecord lcrdr = (LastCellOfRowDummyRecord) record;
        xlsReadSheetHolder.setRowIndex(lcrdr.getRow());
        xlsReadContext.readRowHolder(new ReadRowHolder(lcrdr.getRow(), xlsReadSheetHolder.getTempRowType(), xlsReadContext.readSheetHolder().getGlobalConfiguration(), xlsReadSheetHolder.getCellMap()));
        xlsReadContext.analysisEventProcessor().endRow(xlsReadContext);
        xlsReadSheetHolder.setCellMap(new LinkedHashMap<Integer, Cell>());
        xlsReadSheetHolder.setTempRowType(RowTypeEnum.EMPTY);
    } else if (record instanceof MissingCellDummyRecord) {
        MissingCellDummyRecord mcdr = (MissingCellDummyRecord) record;
        // https://github.com/alibaba/easyexcel/issues/2236
        // Some abnormal XLS, in the case of data already exist, or there will be a "MissingCellDummyRecord"
        // records, so if the existing data, empty data is ignored
        xlsReadSheetHolder.getCellMap().putIfAbsent(mcdr.getColumn(), ReadCellData.newEmptyInstance(mcdr.getRow(), mcdr.getColumn()));
    }
}
Also used : LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) ReadRowHolder(com.alibaba.excel.read.metadata.holder.ReadRowHolder) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) Cell(com.alibaba.excel.metadata.Cell) XlsReadSheetHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder)

Example 5 with ReadRowHolder

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

the class DefaultAnalysisEventProcessor method dealData.

private void dealData(AnalysisContext analysisContext) {
    ReadRowHolder readRowHolder = analysisContext.readRowHolder();
    Map<Integer, ReadCellData<?>> cellDataMap = (Map) readRowHolder.getCellMap();
    readRowHolder.setCurrentRowAnalysisResult(cellDataMap);
    int rowIndex = readRowHolder.getRowIndex();
    int currentHeadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber();
    boolean isData = rowIndex >= currentHeadRowNumber;
    // Last head column
    if (!isData && currentHeadRowNumber == rowIndex + 1) {
        buildHead(analysisContext, cellDataMap);
    }
    // Now is data
    for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
        try {
            if (isData) {
                readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext);
            } else {
                readListener.invokeHead(cellDataMap, analysisContext);
            }
        } catch (Exception e) {
            onException(analysisContext, e);
            break;
        }
        if (!readListener.hasNext(analysisContext)) {
            throw new ExcelAnalysisStopException();
        }
    }
}
Also used : ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) ReadRowHolder(com.alibaba.excel.read.metadata.holder.ReadRowHolder) ExcelAnalysisStopException(com.alibaba.excel.exception.ExcelAnalysisStopException) HashMap(java.util.HashMap) Map(java.util.Map) ReadListener(com.alibaba.excel.read.listener.ReadListener) ExcelAnalysisStopException(com.alibaba.excel.exception.ExcelAnalysisStopException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Aggregations

ReadRowHolder (com.alibaba.excel.read.metadata.holder.ReadRowHolder)6 Cell (com.alibaba.excel.metadata.Cell)5 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)3 RowTypeEnum (com.alibaba.excel.enums.RowTypeEnum)2 XlsReadSheetHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder)2 XlsxReadSheetHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)2 LinkedHashMap (java.util.LinkedHashMap)2 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)1 ExcelAnalysisStopException (com.alibaba.excel.exception.ExcelAnalysisStopException)1 ReadListener (com.alibaba.excel.read.listener.ReadListener)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)1 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)1