使用 POI 以数字格式设置 Excel 单元格值 [英] Setting Excel cell value in Numeric format using POI

查看:111
本文介绍了使用 POI 以数字格式设置 Excel 单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 中收到警告说数字存储为文本...)

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...)

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

推荐答案

您必须提供双精度值.正如文档所说:

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

setCellValue

public void setCellValue(double value)

为单元格设置数值

参数:

value - 将此单元格设置为的数值.对于公式,我们将设置预先计算的值,对于数字,我们将设置其值.对于其他类型我们会将单元格更改为数字单元格并设置其值.

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)

这篇关于使用 POI 以数字格式设置 Excel 单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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