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

The following examples show how to use com.alibaba.excel.metadata.CellData#setBooleanValue() . 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: 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 2
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;
    }
}