Example usage for org.apache.poi.ss.usermodel DataFormatter formatRawCellContents

List of usage examples for org.apache.poi.ss.usermodel DataFormatter formatRawCellContents

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel DataFormatter formatRawCellContents.

Prototype

public String formatRawCellContents(double value, int formatIndex, String formatString) 

Source Link

Document

Formats the given raw cell value, based on the supplied format index and string, according to excel style rules.

Usage

From source file:com.adobe.acs.commons.data.Variant.java

License:Apache License

private void setValue(Cell cell) {
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }// w  w  w  . j a  va2s .  c o  m
    switch (cellType) {
    case Cell.CELL_TYPE_BOOLEAN:
        setValue(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        double number = cell.getNumericCellValue();
        if (Math.floor(number) == number) {
            setValue((long) number);
        } else {
            setValue(number);
        }
        if (DateUtil.isCellDateFormatted(cell)) {
            setValue(cell.getDateCellValue());
        }
        DataFormatter dataFormatter = new DataFormatter();
        if (cellType == Cell.CELL_TYPE_FORMULA) {
            setValue(dataFormatter.formatCellValue(cell));
        } else {
            CellStyle cellStyle = cell.getCellStyle();
            setValue(dataFormatter.formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(),
                    cellStyle.getDataFormatString()));
        }
        break;
    case Cell.CELL_TYPE_STRING:
        setValue(cell.getStringCellValue().trim());
        break;
    case Cell.CELL_TYPE_BLANK:
    default:
        clear();
        break;
    }
}

From source file:org.apache.metamodel.excel.XlsxSheetToRowsHandler.java

License:Apache License

private String createValue() {
    if (_value.length() == 0) {
        return null;
    }//from  w ww . j a v  a2 s  .c o m

    switch (_dataType) {

    case BOOL:
        char first = _value.charAt(0);
        return first == '0' ? "false" : "true";
    case ERROR:
        logger.warn("Error-cell occurred: {}", _value);
        return _value.toString();
    case FORMULA:
        return _value.toString();
    case INLINESTR:
        XSSFRichTextString rtsi = new XSSFRichTextString(_value.toString());
        return rtsi.toString();
    case SSTINDEX:
        String sstIndex = _value.toString();
        int idx = Integer.parseInt(sstIndex);
        XSSFRichTextString rtss = new XSSFRichTextString(_sharedStringTable.getEntryAt(idx));
        return rtss.toString();
    case NUMBER:
        final String numberString = _value.toString();
        if (_formatString != null) {
            DataFormatter formatter = getDataFormatter();
            if (HSSFDateUtil.isADateFormat(_formatIndex, _formatString)) {
                Date date = DateUtil.getJavaDate(Double.parseDouble(numberString));
                return DateUtils.createDateFormat().format(date);
            }
            return formatter.formatRawCellContents(Double.parseDouble(numberString), _formatIndex,
                    _formatString);
        } else {
            if (numberString.endsWith(".0")) {
                // xlsx only stores doubles, so integers get ".0" appended
                // to them
                return numberString.substring(0, numberString.length() - 2);
            }
            return numberString;
        }
    default:
        logger.error("Unsupported data type: {}", _dataType);
        return "";
    }
}