Search in sources :

Example 1 with CellGeneralFormatter

use of org.apache.poi.ss.format.CellGeneralFormatter in project symja_android_library by axkr.

the class XlsxReader method appendValue.

@SuppressWarnings("unchecked")
private Column<?> appendValue(Column<?> column, Cell cell) {
    CellType cellType = cell.getCellType() == FORMULA ? cell.getCachedFormulaResultType() : cell.getCellType();
    switch(cellType) {
        case STRING:
            column.appendCell(cell.getRichStringCellValue().getString());
            return null;
        case NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                // This will return inconsistent results across time zones, but that matches Excel's
                // behavior
                LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                if (column.type() == ColumnType.STRING) {
                    // If column has String type try to honor it and leave the value as an string as similar
                    // as posible as seen in Excel
                    String dataFormatStyle = cell.getCellStyle().getDataFormatString();
                    String val;
                    if ("general".equalsIgnoreCase(dataFormatStyle)) {
                        val = new CellGeneralFormatter().format(cell.getNumericCellValue());
                    } else {
                        val = new CellDateFormatter(dataFormatStyle).format(cell.getDateCellValue());
                    }
                    column.appendCell(val);
                } else {
                    column.appendCell(localDate.toString());
                }
                return null;
            } else {
                double num = cell.getNumericCellValue();
                if (column.type() == ColumnType.INTEGER) {
                    Column<Integer> intColumn = (Column<Integer>) column;
                    if ((int) num == num) {
                        intColumn.append((int) num);
                        return null;
                    } else if ((long) num == num) {
                        Column<Long> altColumn = LongColumn.create(column.name(), column.size());
                        altColumn = intColumn.mapInto(s -> (long) s, altColumn);
                        altColumn.append((long) num);
                        return altColumn;
                    } else {
                        Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
                        altColumn = intColumn.mapInto(s -> (double) s, altColumn);
                        altColumn.append(num);
                        return altColumn;
                    }
                } else if (column.type() == ColumnType.LONG) {
                    Column<Long> longColumn = (Column<Long>) column;
                    if ((long) num == num) {
                        longColumn.append((long) num);
                        return null;
                    } else {
                        Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
                        altColumn = longColumn.mapInto(s -> (double) s, altColumn);
                        altColumn.append(num);
                        return altColumn;
                    }
                } else if (column.type() == ColumnType.DOUBLE) {
                    Column<Double> doubleColumn = (Column<Double>) column;
                    doubleColumn.append(num);
                    return null;
                } else if (column.type() == ColumnType.STRING) {
                    // If column has String type try to honor it and leave the value as an string as similar
                    // as posible as seen in Excel
                    Column<String> stringColumn = (Column<String>) column;
                    String dataFormatStyle = cell.getCellStyle().getDataFormatString();
                    String val;
                    if ("general".equalsIgnoreCase(dataFormatStyle)) {
                        val = new CellGeneralFormatter().format(cell.getNumericCellValue());
                    } else {
                        val = new CellNumberFormatter(dataFormatStyle).format(cell.getNumericCellValue());
                    }
                    stringColumn.append(val);
                }
            }
            break;
        case BOOLEAN:
            if (column.type() == ColumnType.BOOLEAN) {
                Column<Boolean> booleanColumn = (Column<Boolean>) column;
                booleanColumn.append(cell.getBooleanCellValue());
                return null;
            } else if (column.type() == ColumnType.STRING) {
                // If column has String type try to honor it and leave the value as an string as similar
                // as posible as seen in Excel
                Column<String> stringColumn = (Column<String>) column;
                String val = new CellGeneralFormatter().format(cell.getBooleanCellValue());
                stringColumn.append(val);
            }
        default:
            break;
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) CellNumberFormatter(org.apache.poi.ss.format.CellNumberFormatter) Date(java.util.Date) CellDateFormatter(org.apache.poi.ss.format.CellDateFormatter) CellType(org.apache.poi.ss.usermodel.CellType) LongColumn(tech.tablesaw.api.LongColumn) DoubleColumn(tech.tablesaw.api.DoubleColumn) Column(tech.tablesaw.columns.Column) CellGeneralFormatter(org.apache.poi.ss.format.CellGeneralFormatter)

Aggregations

LocalDateTime (java.time.LocalDateTime)1 Date (java.util.Date)1 CellDateFormatter (org.apache.poi.ss.format.CellDateFormatter)1 CellGeneralFormatter (org.apache.poi.ss.format.CellGeneralFormatter)1 CellNumberFormatter (org.apache.poi.ss.format.CellNumberFormatter)1 CellType (org.apache.poi.ss.usermodel.CellType)1 DoubleColumn (tech.tablesaw.api.DoubleColumn)1 LongColumn (tech.tablesaw.api.LongColumn)1 Column (tech.tablesaw.columns.Column)1