Example usage for org.apache.poi.ss.usermodel Cell getNumericCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getNumericCellValue

Introduction

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

Prototype

double getNumericCellValue();

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:com.projectswg.tools.SwgExcelConverter.java

License:Open Source License

private DatatableRow getDataTableRow(Row row, int expectedColumns, String[] types) {
    if (row == null || expectedColumns == 0)
        return null;

    int count = row.getPhysicalNumberOfCells();
    // Use > because empty cells are not considered a "physical cell"
    if (count > expectedColumns) {
        System.err.println("Row " + row.getRowNum() + " has " + count + " cells, expected " + expectedColumns);
        return null;
    }/*from   w w w  .ja  v  a  2 s .  c  o  m*/

    DatatableRow dataRow = new DatatableRow(expectedColumns);

    for (int i = 0; i < expectedColumns; i++) {
        Cell cell = row.getCell(i);
        if (cell == null) { // empty cell
            parseDataEmptyCell(types, dataRow, i);
            continue;
        }

        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            parseDataCell(types, dataRow, i, cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            parseDataCell(types, dataRow, i, cell.getNumericCellValue());
            break;
        default:
            System.out.println("UNK CELL TYPE: " + cell.getCellType());
        }
    }
    return dataRow;
}

From source file:com.qihang.winter.poi.excel.imports.CellValueServer.java

License:Apache License

/**
 * ??/*from  ww  w  . j  a va 2  s  .co  m*/
 * 
 * @param xclass
 * @param cell
 * @param entity
 * @return
 */
private Object getCellValue(String xclass, Cell cell, ExcelImportEntity entity) {
    if (cell == null) {
        return "";
    }
    Object result = null;
    // ?,cell??
    if ("class java.util.Date".equals(xclass) || ("class java.sql.Time").equals(xclass)) {
        if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
            // ?
            result = cell.getDateCellValue();
        } else {
            cell.setCellType(Cell.CELL_TYPE_STRING);
            result = getDateData(entity, cell.getStringCellValue());
        }
        if (("class java.sql.Time").equals(xclass)) {
            result = new Time(((Date) result).getTime());
        }
    } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        result = cell.getNumericCellValue();
    } else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
        result = cell.getBooleanCellValue();
    } else {
        result = cell.getStringCellValue();
    }
    return result;
}

From source file:com.qihang.winter.poi.excel.imports.ExcelImportServer.java

License:Apache License

/**
 * ?key,?????/*  ww w  .ja  v  a2s . co m*/
 *
 * @author Zerrion
 * @date 2013-11-21
 * @param cell
 * @return
 */
private String getKeyValue(Cell cell) {
    Object obj = null;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        obj = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        obj = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        obj = cell.getNumericCellValue();
        break;
    case Cell.CELL_TYPE_FORMULA:
        obj = cell.getCellFormula();
        break;
    }
    return obj == null ? null : obj.toString().trim();
}

From source file:com.qihang.winter.poi.util.PoiSheetUtility.java

License:Apache License

private static void cloneCell(Cell cNew, Cell cOld) {
    cNew.setCellComment(cOld.getCellComment());
    cNew.setCellStyle(cOld.getCellStyle());

    switch (cNew.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN: {
        cNew.setCellValue(cOld.getBooleanCellValue());
        break;/*ww  w . jav  a2 s .c o  m*/
    }
    case Cell.CELL_TYPE_NUMERIC: {
        cNew.setCellValue(cOld.getNumericCellValue());
        break;
    }
    case Cell.CELL_TYPE_STRING: {
        cNew.setCellValue(cOld.getStringCellValue());
        break;
    }
    case Cell.CELL_TYPE_ERROR: {
        cNew.setCellValue(cOld.getErrorCellValue());
        break;
    }
    case Cell.CELL_TYPE_FORMULA: {
        cNew.setCellFormula(cOld.getCellFormula());
        break;
    }
    }

}

From source file:com.quanticate.opensource.spreadsheetexcerpt.excerpt.POIExcerpterAndMerger.java

License:Apache License

private void excerpt(Workbook wb, List<Sheet> sheetsToKeep, OutputStream output) throws IOException {
    // Make the requested sheets be read only
    Set<String> keepNames = new HashSet<String>();
    for (Sheet s : sheetsToKeep) {
        keepNames.add(s.getSheetName());
        for (Row r : s) {
            for (Cell c : r) {
                if (c.getCellType() == Cell.CELL_TYPE_FORMULA) {
                    switch (c.getCachedFormulaResultType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        double vd = c.getNumericCellValue();
                        c.setCellType(Cell.CELL_TYPE_NUMERIC);
                        c.setCellValue(vd);
                        break;
                    case Cell.CELL_TYPE_STRING:
                        RichTextString vs = c.getRichStringCellValue();
                        c.setCellType(Cell.CELL_TYPE_STRING);
                        c.setCellValue(vs);
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        boolean vb = c.getBooleanCellValue();
                        c.setCellType(Cell.CELL_TYPE_BOOLEAN);
                        c.setCellValue(vb);
                        break;
                    case Cell.CELL_TYPE_ERROR:
                        c.setCellType(Cell.CELL_TYPE_BLANK);
                        break;
                    }/*from   w  w  w .j  a  v a2 s.  c o m*/
                }
            }
        }
    }

    // Remove all the other sheets
    // Note - work backwards! Avoids order changing under us
    for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) {
        String name = wb.getSheetName(i);
        if (!keepNames.contains(name)) {
            wb.removeSheetAt(i);
        }
    }

    // Save
    wb.write(output);
}

From source file:com.quanticate.opensource.spreadsheetexcerpt.excerpt.POIExcerpterAndMerger.java

License:Apache License

private void merge(Workbook excerptWB, Workbook fullWB, String[] sheetsToMerge, OutputStream output)
        throws IOException {
    // Identify the sheets in both workbooks
    List<Sheet> sourceSheets = identifySheets(sheetsToMerge, excerptWB);
    List<Sheet> destSheets = identifySheets(sheetsToMerge, fullWB);

    // Process each sheet from the excerpt in turn
    for (int i = 0; i < sheetsToMerge.length; i++) {
        Sheet source = sourceSheets.get(i);
        Sheet dest = destSheets.get(i);/*from   www .java  2 s  .  c om*/

        for (Row srcR : source) {
            for (Cell srcC : srcR) {
                if (srcC.getCellType() == Cell.CELL_TYPE_FORMULA
                        || srcC.getCellType() == Cell.CELL_TYPE_ERROR) {
                    // Don't merge these kinds of cells
                } else {
                    Row destR = dest.getRow(srcR.getRowNum());
                    if (destR == null) {
                        // Newly added row to the excerpt file, skip this
                    } else {
                        Cell destC = destR.getCell(srcC.getColumnIndex());
                        if (destC == null && srcC.getCellType() == Cell.CELL_TYPE_BLANK) {
                            // Both are empty, don't need to do anything
                        } else {
                            if (destC == null)
                                destC = destR.createCell(srcC.getColumnIndex(), srcC.getCellType());

                            // Sync contents
                            if (srcC.getCellType() == Cell.CELL_TYPE_BLANK) {
                                destC.setCellType(Cell.CELL_TYPE_BLANK);
                            } else if (srcC.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                                destC.setCellValue(srcC.getBooleanCellValue());
                            } else if (srcC.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                                destC.setCellValue(srcC.getNumericCellValue());
                            } else if (srcC.getCellType() == Cell.CELL_TYPE_STRING) {
                                destC.setCellValue(srcC.getStringCellValue());
                            }

                            // Sync formatting rules
                            // TODO
                        }
                    }
                }
            }
        }
    }

    // Re-evaluate all the formulas in the destination workbook, now that
    //  we have updated cells in it
    FormulaEvaluator eval = fullWB.getCreationHelper().createFormulaEvaluator();
    eval.evaluateAll();

    // Save the new file
    fullWB.write(output);
}

From source file:com.r573.enfili.common.doc.spreadsheet.SpreadsheetHelper.java

License:Apache License

public static int getCellValueAsInt(Cell cell) {
    int val = (int) Math.round(cell.getNumericCellValue());
    return val;
}

From source file:com.r573.enfili.common.doc.spreadsheet.SpreadsheetHelper.java

License:Apache License

public static long getCellValueAsLong(Cell cell) {
    long val = Math.round(cell.getNumericCellValue());
    return val;
}

From source file:com.r573.enfili.common.doc.spreadsheet.SpreadsheetHelper.java

License:Apache License

public static double getCellValueAsDouble(Cell cell) {
    double val = cell.getNumericCellValue();
    return val;
}

From source file:com.r573.enfili.common.doc.spreadsheet.SpreadsheetHelper.java

License:Apache License

public static float getCellValueAsFloat(Cell cell) {
    float val = (float) cell.getNumericCellValue();
    return val;
}