Java Code Examples for com.alibaba.excel.metadata.CellData#setType()

The following examples show how to use com.alibaba.excel.metadata.CellData#setType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CellValueTagHandler.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
@Override
protected void setStringValue(XlsxReadContext xlsxReadContext) {
    // Have to go "sharedStrings.xml" and get it
    CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData();
    switch (tempCellData.getType()) {
        case STRING:
            String stringValue = xlsxReadContext.readWorkbookHolder().getReadCache()
                .get(Integer.valueOf(tempCellData.getStringValue()));
            if (stringValue != null && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
                stringValue = stringValue.trim();
            }
            tempCellData.setStringValue(stringValue);
            break;
        case DIRECT_STRING:
            tempCellData.setType(CellDataTypeEnum.STRING);
            break;
        default:
    }
}
 
Example 2
Source File: CellDataDataTest.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
private List<CellDataData> data() throws Exception {
    List<CellDataData> list = new ArrayList<CellDataData>();
    CellDataData cellDataData = new CellDataData();
    cellDataData.setDate(new CellData<Date>(DateUtils.parseDate("2020-01-01 01:01:01")));
    CellData<Integer> integer1 = new CellData<Integer>();
    integer1.setType(CellDataTypeEnum.NUMBER);
    integer1.setNumberValue(BigDecimal.valueOf(2L));
    cellDataData.setInteger1(integer1);
    cellDataData.setInteger2(2);
    CellData formulaValue = new CellData();
    formulaValue.setFormula(Boolean.TRUE);
    formulaValue.setFormulaValue("B2+C2");
    cellDataData.setFormulaValue(formulaValue);
    list.add(cellDataData);
    return list;
}
 
Example 3
Source File: AbstractCellValueTagHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    CellData tempCellData = xlsxReadSheetHolder.getTempCellData();
    StringBuilder tempData = xlsxReadSheetHolder.getTempData();
    CellDataTypeEnum oldType = tempCellData.getType();
    switch (oldType) {
        case DIRECT_STRING:
        case STRING:
        case ERROR:
            tempCellData.setStringValue(tempData.toString());
            break;
        case BOOLEAN:
            tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString()));
            break;
        case NUMBER:
        case EMPTY:
            tempCellData.setType(CellDataTypeEnum.NUMBER);
            tempCellData.setNumberValue(new BigDecimal(tempData.toString()));
            break;
        default:
            throw new IllegalStateException("Cannot set values now");
    }

    // set string value
    setStringValue(xlsxReadContext);

    if (tempCellData.getStringValue() != null
        && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
        tempCellData.setStringValue(tempCellData.getStringValue());
    }

    tempCellData.checkEmpty();
    xlsxReadSheetHolder.getCellMap().put(xlsxReadSheetHolder.getColumnIndex(), tempCellData);
}
 
Example 4
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) {
    if (value == null) {
        return new CellData(CellDataTypeEnum.EMPTY);
    }
    if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
        value = ((String)value).trim();
    }
    CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty);
    if (cellData.getFormula() != null && cellData.getFormula()) {
        cell.setCellFormula(cellData.getFormulaValue());
    }
    if (cellData.getType() == null) {
        cellData.setType(CellDataTypeEnum.EMPTY);
    }
    WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
    switch (cellData.getType()) {
        case STRING:
            cell.setCellValue(cellData.getStringValue());
            return cellData;
        case BOOLEAN:
            cell.setCellValue(cellData.getBooleanValue());
            return cellData;
        case NUMBER:
            cell.setCellValue(cellData.getNumberValue().doubleValue());
            return cellData;
        case IMAGE:
            setImageValue(cellData, cell);
            return cellData;
        case EMPTY:
            return cellData;
        default:
            throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData,
                excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType()
                    + "at row:" + cell.getRow().getRowNum());
    }
}
 
Example 5
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty) {
    if (value == null) {
        return new CellData(CellDataTypeEnum.EMPTY);
    }
    // This means that the user has defined the data.
    if (value instanceof CellData) {
        CellData cellDataValue = (CellData)value;
        if (cellDataValue.getType() != null) {
            return cellDataValue;
        } else {
            if (cellDataValue.getData() == null) {
                cellDataValue.setType(CellDataTypeEnum.EMPTY);
                return cellDataValue;
            }
        }
        CellData cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), cell,
            cellDataValue.getData(), excelContentProperty);
        // The formula information is subject to user input
        if (cellDataValue.getFormula() != null) {
            cellDataReturn.setFormula(cellDataValue.getFormula());
            cellDataReturn.setFormulaValue(cellDataValue.getFormulaValue());
        }
        return cellDataReturn;
    }
    return doConvert(currentWriteHolder, clazz, cell, value, excelContentProperty);
}
 
Example 6
Source File: FormulaRecordHandler.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    FormulaRecord frec = (FormulaRecord)record;
    Map<Integer, Cell> cellMap = xlsReadContext.xlsReadSheetHolder().getCellMap();
    CellData tempCellData = new CellData();
    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) {
        LOGGER.debug("Get formula value error.", e);
    }
    tempCellData.setFormula(Boolean.TRUE);
    tempCellData.setFormulaValue(formulaValue);
    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()));
            Integer dataFormat =
                xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatIndex(frec);
            tempCellData.setDataFormat(dataFormat);
            tempCellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
                xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatString(frec),
                xlsReadContext.readSheetHolder().getGlobalConfiguration().getLocale()));
            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;
    }
}