java - 使用 POI 以数字格式设置 Excel 单元格值

标签 java excel apache-poi

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

我试过下面的代码,但它没有转换为数字类型(我在 excel 中收到警告,说数字存储为文本...)

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

最佳答案

您必须提供 double 值。作为the doc says :

setCellValue

public void setCellValue(double value)

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.

所以,应该是

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

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

关于java - 使用 POI 以数字格式设置 Excel 单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8967942/

相关文章:

Java将整数转换为字符串并计算

java - 在 Applet 中显示 JList

java - 我们可以使用java更新受密码保护的Excel工作表的行和列吗?

Java 无法用于 Opencv 安装

java - C# Tcp 客户端无法向 android 服务器发送消息

python - 具有混合时间和日期时间的列的 Pandas 问题

excel - VBA Excel - 无法在重叠选择上使用该命令

vba - 将数据复制并粘贴到新工作表后如何更改子范围

java - Apache POI 解析和处理空单元格

java - 使用 POI 库进行条件格式化