[英]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.我想将数字单元格的值作为一个简单的字符串。
Suppose there the type of cell is numeric with value 90%
.假设单元格的类型是数值为
90%
数值。 Now I cannot use cell.getStringCellValue()
as it will throw exception.现在我不能使用
cell.getStringCellValue()
因为它会抛出异常。 I also cannot use cell.getNumericCellValue()
as it will return me .9
and not 90%
.我也不能使用
cell.getNumericCellValue()
因为它会返回我.9
而不是90%
。
I want to store in db which is of type varchar2, so I want the value in string only.我想存储在varchar2类型的db中,所以我只想要字符串中的值。
I cannot change the cell type in xls
as its the end user job, I have to handle this in code itself.我无法将
xls
的单元格类型更改为最终用户的工作,我必须在代码本身中处理这个问题。
Also formatter does't work well as there could be different cell types in the xls...dd:mm,dd:mm:ss,formula etc.格式化程序也不能很好地工作,因为 xls...dd:mm,dd:mm:ss,formula 等中可能有不同的单元格类型。
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));
will return "90%"将返回“90%”
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此方法的 API 位于http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html#formatCellValue%28org.apache.poi.ss.usermodel.Cell%29
This works directly even with an HSSFCell即使使用 HSSFCell 也可以直接使用
it worked for me even when my Cell is an HSSFCell即使我的 Cell 是 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));
Try试试
cell.getRichStringCellValue ().getString();
The following code is using current apache poi
versions of 2021. Now DataFormatter can be used for XSSF
(Office Open XML *.xlsx
) as well as for HSSF
(BIFF *.xls
) formats.以下代码使用 2021 年的当前
apache poi
版本。现在DataFormatter可用于XSSF
(Office Open XML *.xlsx
)以及HSSF
(BIFF *.xls
)格式。 It should be used together with FormulaEvaluator to get values from formula cells too.它也应该与FormulaEvaluator一起使用以从公式单元格中获取值。
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
class ReadExcel {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("Excel.xlsx"));
//Workbook workbook = WorkbookFactory.create(new FileInputStream("Excel.xls"));
DataFormatter dataFormatter = new DataFormatter(java.util.Locale.US);
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
String cellValue = "";
for (Sheet sheet: workbook) {
System.out.println(sheet.getSheetName());
for (Row row : sheet) {
for (Cell cell : row) {
cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);
System.out.println(cell.getAddress() + ":" + cellValue);
// do something with cellValue
}
}
}
workbook.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.