Example usage for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue

Introduction

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

Prototype

public double getNumericCellValue() 

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:utils.ReadWriteExcelFile.java

public static void readXLSFile(String aFile) throws IOException {
    InputStream ExcelFileToRead = new FileInputStream(aFile);
    HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);

    HSSFSheet sheet = wb.getSheetAt(0);/*from w w w.java2  s .  c  o m*/
    HSSFRow row;
    HSSFCell cell;

    Iterator rows = sheet.rowIterator();

    while (rows.hasNext()) {
        row = (HSSFRow) rows.next();
        Iterator cells = row.cellIterator();

        while (cells.hasNext()) {
            cell = (HSSFCell) cells.next();

            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                System.out.print(cell.getStringCellValue() + " ");
            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                System.out.print(cell.getNumericCellValue() + " ");
            } else {
                //U Can Handel Boolean, Formula, Errors
            }
        }
        System.out.println();
    }

}

From source file:vone.HSSFReadWrite.java

License:Apache License

/**
 * Method main//from ww  w . j a  va  2  s. c  o m
 *
 * Given 1 argument takes that as the filename, inputs it and dumps the cell
 * values/types out to sys.out.<br/>
 *
 * given 2 arguments where the second argument is the word "write" and the
 * first is the filename - writes out a sample (test) spreadsheet see
 * {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/>
 *
 * given 2 arguments where the first is an input filename and the second an
 * output filename (not write), attempts to fully read in the spreadsheet
 * and fully write it out.<br/>
 *
 * given 3 arguments where the first is an input filename and the second an
 * output filename (not write) and the third is "modify1", attempts to read
 * in the spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col
 * 3 to "MODIFIED CELL" then writes it out. Hence this is "modify test 1".
 * If you take the output from the write test, you'll have a valid scenario.
 */
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("At least one argument expected");
        return;
    }

    String fileName = args[0];
    try {
        if (args.length < 2) {

            HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);

            System.out.println("Data dump:\n");

            for (int k = 0; k < wb.getNumberOfSheets(); k++) {
                HSSFSheet sheet = wb.getSheetAt(k);
                int rows = sheet.getPhysicalNumberOfRows();
                System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s).");
                for (int r = 0; r < rows; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }

                    // int cells = row.getPhysicalNumberOfCells();
                    int cells = row.getLastCellNum();
                    System.out.println("\nROW " + row.getRowNum() + " has " + cells + " cell(s).");
                    for (int c = 0; c < cells; c++) {
                        HSSFCell cell = row.getCell(c);
                        String value = null;
                        if (cell == null) {
                            System.out.println("CELL col=" + c + " VALUE= Empty");
                            continue;
                        }

                        switch (cell.getCellTypeEnum()) {

                        case FORMULA:
                            value = "FORMULA value=" + cell.getCellFormula();
                            break;

                        case NUMERIC:
                            value = "NUMERIC value=" + cell.getNumericCellValue();
                            break;

                        case STRING:
                            value = "STRING value=" + cell.getStringCellValue();
                            break;

                        default:
                        }
                        System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
                    }
                }
            }
            wb.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}