Search in sources :

Example 1 with RowTypeEnum

use of com.alibaba.excel.enums.RowTypeEnum 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 2 with RowTypeEnum

use of com.alibaba.excel.enums.RowTypeEnum 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)

Aggregations

RowTypeEnum (com.alibaba.excel.enums.RowTypeEnum)2 Cell (com.alibaba.excel.metadata.Cell)2 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)2 ReadRowHolder (com.alibaba.excel.read.metadata.holder.ReadRowHolder)2 XlsxReadSheetHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)1 LinkedHashMap (java.util.LinkedHashMap)1