如何获取 Apache POI 中单元格数字的格式化值? [英] How to get the formatted value of a number for a cell in Apache POI?

查看:38
本文介绍了如何获取 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.

推荐答案

您可以使用以下方法强制将值作为字符串返回

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 位于 http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html#formatCellValue%28org.apache.poi.ss.usermodel.Cell%29

The API for this method is at http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html#formatCellValue%28org.apache.poi.ss.usermodel.Cell%29

即使使用 HSSFCell 也可以直接使用

This works directly even with an HSSFCell

即使我的 Cell 是 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屋!

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