如何从Excel中的Excel中读取数值? [英] How to read numeric values from excel in java?
本文介绍了如何从Excel中的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();
这篇关于如何从Excel中的Excel中读取数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文