Search in sources :

Example 6 with Cell

use of com.alibaba.excel.metadata.Cell in project easyexcel by alibaba.

the class FormulaRecordHandler method processRecord.

@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    FormulaRecord frec = (FormulaRecord) record;
    Map<Integer, Cell> cellMap = xlsReadContext.xlsReadSheetHolder().getCellMap();
    ReadCellData<?> tempCellData = new ReadCellData<>();
    tempCellData.setRowIndex(frec.getRow());
    tempCellData.setColumnIndex((int) frec.getColumn());
    CellType cellType = CellType.forInt(frec.getCachedResultType());
    String formulaValue = null;
    try {
        formulaValue = HSSFFormulaParser.toFormulaString(xlsReadContext.xlsReadWorkbookHolder().getHssfWorkbook(), frec.getParsedExpression());
    } catch (Exception e) {
        log.debug("Get formula value error.", e);
    }
    FormulaData formulaData = new FormulaData();
    formulaData.setFormulaValue(formulaValue);
    tempCellData.setFormulaData(formulaData);
    xlsReadContext.xlsReadSheetHolder().setTempRowType(RowTypeEnum.DATA);
    switch(cellType) {
        case STRING:
            // Formula result is a string
            // This is stored in the next record
            tempCellData.setType(CellDataTypeEnum.STRING);
            xlsReadContext.xlsReadSheetHolder().setTempCellData(tempCellData);
            break;
        case NUMERIC:
            tempCellData.setType(CellDataTypeEnum.NUMBER);
            tempCellData.setNumberValue(BigDecimal.valueOf(frec.getValue()));
            int dataFormat = xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatIndex(frec);
            DataFormatData dataFormatData = new DataFormatData();
            dataFormatData.setIndex((short) dataFormat);
            dataFormatData.setFormat(BuiltinFormats.getBuiltinFormat(dataFormatData.getIndex(), xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatString(frec), xlsReadContext.readSheetHolder().getGlobalConfiguration().getLocale()));
            tempCellData.setDataFormatData(dataFormatData);
            cellMap.put((int) frec.getColumn(), tempCellData);
            break;
        case ERROR:
            tempCellData.setType(CellDataTypeEnum.ERROR);
            tempCellData.setStringValue(ERROR);
            cellMap.put((int) frec.getColumn(), tempCellData);
            break;
        case BOOLEAN:
            tempCellData.setType(CellDataTypeEnum.BOOLEAN);
            tempCellData.setBooleanValue(frec.getCachedBooleanValue());
            cellMap.put((int) frec.getColumn(), tempCellData);
            break;
        default:
            tempCellData.setType(CellDataTypeEnum.EMPTY);
            cellMap.put((int) frec.getColumn(), tempCellData);
            break;
    }
}
Also used : ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) CellType(org.apache.poi.ss.usermodel.CellType) FormulaData(com.alibaba.excel.metadata.data.FormulaData) DataFormatData(com.alibaba.excel.metadata.data.DataFormatData) Cell(com.alibaba.excel.metadata.Cell)

Example 7 with Cell

use of com.alibaba.excel.metadata.Cell in project easyexcel by alibaba.

the class RowTagHandler method startElement.

@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    int rowIndex = PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_R), xlsxReadSheetHolder.getRowIndex());
    Integer lastRowIndex = xlsxReadContext.readSheetHolder().getRowIndex();
    while (lastRowIndex + 1 < rowIndex) {
        xlsxReadContext.readRowHolder(new ReadRowHolder(lastRowIndex + 1, RowTypeEnum.EMPTY, xlsxReadSheetHolder.getGlobalConfiguration(), new LinkedHashMap<Integer, Cell>()));
        xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
        xlsxReadSheetHolder.setColumnIndex(null);
        xlsxReadSheetHolder.setCellMap(new LinkedHashMap<Integer, Cell>());
        lastRowIndex++;
    }
    xlsxReadSheetHolder.setRowIndex(rowIndex);
}
Also used : XlsxReadSheetHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder) ReadRowHolder(com.alibaba.excel.read.metadata.holder.ReadRowHolder) Cell(com.alibaba.excel.metadata.Cell) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Cell (com.alibaba.excel.metadata.Cell)7 ReadRowHolder (com.alibaba.excel.read.metadata.holder.ReadRowHolder)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 ReadCache (com.alibaba.excel.cache.ReadCache)1 DataFormatData (com.alibaba.excel.metadata.data.DataFormatData)1 FormulaData (com.alibaba.excel.metadata.data.FormulaData)1 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)1 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)1 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)1 CellType (org.apache.poi.ss.usermodel.CellType)1