How to read numeric values from excel in java?

元气小坏坏 提交于 2019-12-25 00:43:02

问题


I have a code to read a xls file in java.
But the problem is that when I am read the numeric value that time it gives me wrong format value. My code is

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);
                }
        }

I have value 119710179 this is numeric but when I am syso that time it display cellvalue--1.19710179E8

I used this code also but same numeric 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);
                    }
            }

All values are display properly but this only one value get wrong. Thanks in advance


回答1:


To change cell type:

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

And now you can read from this cell a string:

myCell.getStringCellValue();



回答2:


here is some formatting example:

        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));


来源:https://stackoverflow.com/questions/27688250/how-to-read-numeric-values-from-excel-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!