如何从java中的excel读取数值?

时间:2014-12-29 11:45:44

标签: java apache-poi

我有一个代码来读取java中的xls文件。
但问题是,当我读取数值时,它给了我错误的格式值。 我的代码是

String fname = "D:/Vijay/BRS_docs/10168/19.11.2014/19.11.2014/Bank/Debit Open Item File/INF 21 Case.xls";
FileInputStream fis = new FileInputStream(fname);
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = (HSSFSheet) wb.getSheetAt(0);
        FormulaEvaluator formulaEval = wb.getCreationHelper().createFormulaEvaluator();
        fis = new FileInputStream(fname);
        Iterator rowIter = sheet.rowIterator();
        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();
                while (cellIter.hasNext()) {
                    String cellvalue = "";
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                        cellvalue = ""+ formulaEval.evaluate(myCell).formatAsString();
                        System.out.println("cellvalue--" + cellvalue);
                }
        }

我有价值119710179这是数字,但当我是syso时,它显示 cellvalue - 1.19710179E8

我也使用了这个代码,但使用了相同的数字fomat

Iterator cellIter = myRow.cellIterator();

                while (cellIter.hasNext()) {
                    String cellvalue = "";
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                        if (myCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            System.out.println("11");
                            cellvalue = myCell.getStringCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                            System.out.println("22");
                             if (DateUtil.isCellDateFormatted(myCell)) {
                                 cellvalue = myCell.getDateCellValue().toString();
                                } else {
                                    cellvalue = Double.toString(myCell.getNumericCellValue());
                                }
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                            System.out.println("33");
                            cellvalue = "" + myCell.getBooleanCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                            System.out.println("44");
                            cellvalue = ""+ formulaEval.evaluate(myCell).formatAsString();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                            System.out.println("55");
                            cellvalue = "";
                        }
                        System.out.println("cellvalue--" + cellvalue);
                    }
            }

所有值都正确显示但只有一个值出错。 提前致谢

2 个答案:

答案 0 :(得分:2)

这是一些格式化示例:

        Workbook wb = new HSSFWorkbook();
        // Workbook wb = new XSSFWorkbook();
        CreationHelper createHelper = wb.getCreationHelper();
        Sheet sheet = wb.createSheet("new sheet");

        // Create a row and put some cells in it. Rows are 0 based.
        Row row = sheet.createRow((short) 0);
        // Create a cell and put a value in it.
        Cell cell = row.createCell(0);
        cell.setCellValue(119710179);

        // Or do it on one line.
        row.createCell(1).setCellValue(119710179);
        row.createCell(2).setCellValue(createHelper.createRichTextString("119710179 "));
        row.createCell(3).setCellValue(true);

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();

        String cellvalue = "" + cell.getNumericCellValue();

        DataFormatter formatter = new DataFormatter(Locale.US);

        System.out.println("cellvalue--" + formatter.formatCellValue(cell));

答案 1 :(得分:0)

更改细胞类型:

if(myCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        myCell.setCellType(Cell.CELL_TYPE_STRING);
}

现在你可以从这个单元格中读取一个字符串:

myCell.getStringCellValue();