如何从java中的excel读取数值? [英] How to read numeric values from excel in java?

查看:36
本文介绍了如何从java中的excel读取数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

更改单元格类型:

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

这篇关于如何从java中的excel读取数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆