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

The following examples show how to use com.alibaba.excel.metadata.CellData#setStringValue() . 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: CellInlineStringValueTagHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
protected void setStringValue(XlsxReadContext xlsxReadContext) {
    // This is a special form of string
    CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData();
    XSSFRichTextString richTextString = new XSSFRichTextString(tempCellData.getStringValue());
    tempCellData.setStringValue(richTextString.toString());
}
 
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: StringRecordHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    // String for formula
    StringRecord srec = (StringRecord)record;
    XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
    CellData tempCellData = xlsReadSheetHolder.getTempCellData();
    if (tempCellData == null) {
        LOGGER.warn("String type formula but no value found.");
        return;
    }
    tempCellData.setStringValue(srec.getString());
    xlsReadSheetHolder.getCellMap().put(tempCellData.getColumnIndex(), tempCellData);
    xlsReadSheetHolder.setTempCellData(null);
}
 
Example 5
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;
    }
}