如何获取Apache POI中单元格的数字格式值? [英] How to get the formatted value of a number for a cell in Apache POI?
问题描述
我想以一个简单的字符串来获取数值单元格的值.
I wanted to get the value of a Numeric cell as a simple string.
假定那里的单元格类型是数值为90%
的数字.
现在我不能使用cell.getStringCellValue()
,因为它会引发异常.
我也不能使用cell.getNumericCellValue()
,因为它将返回我.9
而不是90%
.
Suppose there the type of cell is numeric with value 90%
.
Now I cannot use cell.getStringCellValue()
as it will throw exception.
I also cannot use cell.getNumericCellValue()
as it will return me .9
and not 90%
.
我想存储在varchar2类型的db中,所以我只需要字符串中的值.
I want to store in db which is of type varchar2, so I want the value in string only.
我不能更改xls
中的单元格类型,因为它是最终用户的工作,我必须在代码本身中进行处理.
I cannot change the cell type in xls
as its the end user job, I have to handle this in code itself.
格式化程序也无法正常工作,因为xls ... dd:mm,dd:mm:ss,公式等中可能存在不同的单元格类型.
Also formatter does't work well as there could be different cell types in the xls...dd:mm,dd:mm:ss,formula etc.
我想要的是,无论单元格类型是什么,我都需要获取其值为简单字符串的值.
All I want is that whatever the cell type is I need to get its value as simple String.
推荐答案
您可以使用以下方法强制将值作为String返回
You can force the value to be returned as a String using the methods below
HSSFDataFormatter hdf = new HSSFDataFormatter();
System.out.println (hdf.formatCellValue(mycell));
将返回"90%"
此方法的API位于即使在HSSFCell上,它也可以直接工作
This works directly even with an HSSFCell
即使我的手机是HSSFCell,它也对我有用
it worked for me even when my Cell is an HSSFCell
我也尝试过这种演员表-可以.
i've also tried this cast - which works.
HSSFCell cell1 = (HSSFCell) row1.getCell(2);
HSSFDataFormatter hdf = new HSSFDataFormatter();
System.out.println ("formatted "+ hdf.formatCellValue(cell1));
这篇关于如何获取Apache POI中单元格的数字格式值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!