阅读货币&% 使用 Java [英] Read currency & % using Java
问题描述
我正在尝试使用 Apache Poi (v3.11/XSSFWorkbook) 读取 excel 文件.我正在阅读特定的工作表 {e.x.sheet(0)} 由来自同一文件的大约 15 个工作表的不同宏组成.问题是我能够读取非格式化字符串数据,但无法读取货币或 % 等格式化数据.
I am trying to read an excel file using Apache Poi (v3.11 / XSSFWorkbook ). I am reading a particular sheet {e.x. sheet(0)} which is formed by different macros from around 15 sheets from the same file. The issue is I am able to read the non formatted String data but unable to read formatted data like currency or %.
反正我不控制excel工作表.我将从网络共享路径读取文件.
I do not control the excel worksheet anyway. I will be reading the file from a network share path.
XSSFWorkbook myWorkBook = null
XSSFSheet mySheet = null;
Row rowHeader = null;
Row rowData = null;
FileInputStream fis = new FileInputStream(
new File(
"inputfile.xlsx"));
myWorkBook = new XSSFWorkbook(fis);
mySheet = myWorkBook.getSheetAt(0);
rowHeader = mySheet.getRow(0);
rowData = mySheet.getRow(1);
for (Cell s : rowHeader) {
System.out.print("| "+s.getStringCellValue());
System.out.println(" -->"+getCellValue(myWorkBook,rowData.getCell(s.getColumnIndex())));
}
myWorkBook.close();
}
// $#,##0.00
private static String getCellValue(XSSFWorkbook wb, Cell cell)
{
XSSFCell x = (XSSFCell)cell;
return x.getRawValue();
}
}
我有一个像下面这样的excel表
I have a excel sheet like below
Name Amount Rate
Name1 $1,500 75%
如果我使用此代码阅读上述表格
If I read the above sheet using this code
姓名 --> 姓名 1
Name --> Name1
金额 --> 1500(注意缺少的 $ 和 , )
Amount --> 1500 ( note the missing $ and , )
Rate --> 74.99999998(数字不准确且缺少 % 符号)
Rate --> 74.99999998 (number inacurate and missing % symbol)
推荐答案
感谢@Gagravarr
Thanks @Gagravarr
下面的代码解决了这个问题
this below code solved the issue
private static String getCellValue(XSSFWorkbook wb, Cell cell)
{
DataFormatter formatter = new DataFormatter();
String formattedCellValue = formatter.formatCellValue(cell);
return formattedCellValue;
}
这篇关于阅读货币&% 使用 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!