Search in sources :

Example 6 with ReadCellData

use of com.alibaba.excel.metadata.data.ReadCellData 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 ReadCellData

use of com.alibaba.excel.metadata.data.ReadCellData 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

ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)7 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)3 Cell (com.alibaba.excel.metadata.Cell)3 ReadRowHolder (com.alibaba.excel.read.metadata.holder.ReadRowHolder)3 Map (java.util.Map)3 RowTypeEnum (com.alibaba.excel.enums.RowTypeEnum)2 Head (com.alibaba.excel.metadata.Head)2 ReadListener (com.alibaba.excel.read.listener.ReadListener)2 ExcelReadHeadProperty (com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)2 EasyExcel (com.alibaba.excel.EasyExcel)1 ExcelWriter (com.alibaba.excel.ExcelWriter)1 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 ReadConverterContext (com.alibaba.excel.converters.ReadConverterContext)1 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)1 ExcelAnalysisStopException (com.alibaba.excel.exception.ExcelAnalysisStopException)1 DataFormatData (com.alibaba.excel.metadata.data.DataFormatData)1 FormulaData (com.alibaba.excel.metadata.data.FormulaData)1 ModelBuildEventListener (com.alibaba.excel.read.listener.ModelBuildEventListener)1 ReadSheetHolder (com.alibaba.excel.read.metadata.holder.ReadSheetHolder)1 XlsxReadSheetHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)1