Search in sources :

Example 6 with DataFormatData

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

use of com.alibaba.excel.metadata.data.DataFormatData in project easyexcel by alibaba.

the class StyleProperty method build.

public static StyleProperty build(ContentStyle contentStyle) {
    if (contentStyle == null) {
        return null;
    }
    StyleProperty styleProperty = new StyleProperty();
    if (contentStyle.dataFormat() >= 0) {
        DataFormatData dataFormatData = new DataFormatData();
        dataFormatData.setIndex(contentStyle.dataFormat());
        styleProperty.setDataFormatData(dataFormatData);
    }
    styleProperty.setHidden(contentStyle.hidden().getBooleanValue());
    styleProperty.setLocked(contentStyle.locked().getBooleanValue());
    styleProperty.setQuotePrefix(contentStyle.quotePrefix().getBooleanValue());
    styleProperty.setHorizontalAlignment(contentStyle.horizontalAlignment().getPoiHorizontalAlignment());
    styleProperty.setWrapped(contentStyle.wrapped().getBooleanValue());
    styleProperty.setVerticalAlignment(contentStyle.verticalAlignment().getPoiVerticalAlignmentEnum());
    if (contentStyle.rotation() >= 0) {
        styleProperty.setRotation(contentStyle.rotation());
    }
    if (contentStyle.indent() >= 0) {
        styleProperty.setIndent(contentStyle.indent());
    }
    styleProperty.setBorderLeft(contentStyle.borderLeft().getPoiBorderStyle());
    styleProperty.setBorderRight(contentStyle.borderRight().getPoiBorderStyle());
    styleProperty.setBorderTop(contentStyle.borderTop().getPoiBorderStyle());
    styleProperty.setBorderBottom(contentStyle.borderBottom().getPoiBorderStyle());
    if (contentStyle.leftBorderColor() >= 0) {
        styleProperty.setLeftBorderColor(contentStyle.leftBorderColor());
    }
    if (contentStyle.rightBorderColor() >= 0) {
        styleProperty.setRightBorderColor(contentStyle.rightBorderColor());
    }
    if (contentStyle.topBorderColor() >= 0) {
        styleProperty.setTopBorderColor(contentStyle.topBorderColor());
    }
    if (contentStyle.bottomBorderColor() >= 0) {
        styleProperty.setBottomBorderColor(contentStyle.bottomBorderColor());
    }
    styleProperty.setFillPatternType(contentStyle.fillPatternType().getPoiFillPatternType());
    if (contentStyle.fillBackgroundColor() >= 0) {
        styleProperty.setFillBackgroundColor(contentStyle.fillBackgroundColor());
    }
    if (contentStyle.fillForegroundColor() >= 0) {
        styleProperty.setFillForegroundColor(contentStyle.fillForegroundColor());
    }
    styleProperty.setShrinkToFit(contentStyle.shrinkToFit().getBooleanValue());
    return styleProperty;
}
Also used : DataFormatData(com.alibaba.excel.metadata.data.DataFormatData)

Aggregations

DataFormatData (com.alibaba.excel.metadata.data.DataFormatData)7 Cell (com.alibaba.excel.metadata.Cell)1 Head (com.alibaba.excel.metadata.Head)1 FormulaData (com.alibaba.excel.metadata.data.FormulaData)1 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)1 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)1 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)1 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)1 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 NumberRecord (org.apache.poi.hssf.record.NumberRecord)1 CellType (org.apache.poi.ss.usermodel.CellType)1 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)1 Test (org.junit.Test)1