使用POI以数字格式设置Excel计算单元值

时间:2022-09-17 20:25:40

I want to store numeric data in excel sheet.
The numeric data is represented by String type in my java code.
Is it possible to set it as numeric without casting it?

我想在excel表格中存储数字数据。在我的java代码中,数字数据由字符串类型表示。是否可以将它设置为数值而不进行强制转换?

I've tried the following code, but it wasn't converted to numeric type (I get a warning in excel saying that number is stored as text...)

我尝试过下面的代码,但是它没有转换成数字类型(我在excel中得到一个警告,说这个数字存储为文本…)

HSSFCell dCell =...
dCell.setCellType(Cell.CELL_TYPE_NUMERIC);
dCell.setCellValue("112.45")

4 个解决方案

#1


21  

You have to provide the value as a double. As the doc says:

你必须提供双倍的值。就像医生说的:

setCellValue

setCellValue

public void setCellValue(double value)

公共空间setCellValue(双值)

set a numeric value for the cell

为单元格设置数值

Parameters:

参数:

value - the numeric value to set this cell to. For formulas we'll set the precalculated value, for numerics we'll set its value. For other types we will change the cell to a numeric cell and set its value.

值——设置此单元格的数值。对于公式,我们将设置预计算值,对于数值计算,我们将设置它的值。对于其他类型,我们将把单元格更改为数字单元格并设置其值。

So, it should be

所以,它应该是

dCell.setCellValue(new Double("123"));

OR

dCell.setCellValue(123);  //Remember, the way you did was, you actually passed a string (no quotes)

#2


8  

I used Big Decimal for this,

我用了大小数点,

dCell.setCellValue(new BigDecimal("112.45").doubleValue());

#3


2  

Try to use wrapper classes:

尝试使用包装类:

dCell.setCellValue(new Double(112.45));

BTW, it will work if you simply give the value instead of the double cotes as follows:

顺便说一句,如果你只是简单地给出价值,而不是像下面这样给出双cotes,它就会起作用:

dCell.setCellValue(112.45);

#4


1  

Old one But Still it This will help Some one from .NET who use NPOI

旧的,但这仍然会帮助。net的一些用户使用NPOI

dCell.setCellValue(Convert.ToDouble(112.45));

We do have

我们确实有

cell.SetCellType(NPOI.SS.UserModel.CellType.NUMERIC); 

which doesn't remove the warning but the 1st code did worked after converting to Double for All integers and Double values in .net

这并没有删除警告,但是第一段代码在将所有整数和。net中的双值转换为双精度后仍然有效

#1


21  

You have to provide the value as a double. As the doc says:

你必须提供双倍的值。就像医生说的:

setCellValue

setCellValue

public void setCellValue(double value)

公共空间setCellValue(双值)

set a numeric value for the cell

为单元格设置数值

Parameters:

参数:

value - the numeric value to set this cell to. For formulas we'll set the precalculated value, for numerics we'll set its value. For other types we will change the cell to a numeric cell and set its value.

值——设置此单元格的数值。对于公式,我们将设置预计算值,对于数值计算,我们将设置它的值。对于其他类型,我们将把单元格更改为数字单元格并设置其值。

So, it should be

所以,它应该是

dCell.setCellValue(new Double("123"));

OR

dCell.setCellValue(123);  //Remember, the way you did was, you actually passed a string (no quotes)

#2


8  

I used Big Decimal for this,

我用了大小数点,

dCell.setCellValue(new BigDecimal("112.45").doubleValue());

#3


2  

Try to use wrapper classes:

尝试使用包装类:

dCell.setCellValue(new Double(112.45));

BTW, it will work if you simply give the value instead of the double cotes as follows:

顺便说一句,如果你只是简单地给出价值,而不是像下面这样给出双cotes,它就会起作用:

dCell.setCellValue(112.45);

#4


1  

Old one But Still it This will help Some one from .NET who use NPOI

旧的,但这仍然会帮助。net的一些用户使用NPOI

dCell.setCellValue(Convert.ToDouble(112.45));

We do have

我们确实有

cell.SetCellType(NPOI.SS.UserModel.CellType.NUMERIC); 

which doesn't remove the warning but the 1st code did worked after converting to Double for All integers and Double values in .net

这并没有删除警告,但是第一段代码在将所有整数和。net中的双值转换为双精度后仍然有效