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

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

问题描述

我要存储在Excel工作表的数值数据。结果
数字数据是通过重新字符串类型在我的Java code psented $ P $。结果
是否有可能将其设置为数字而无需进行转换呢?

我试过以下code,但它并没有转换为数字类型(我得到在Excel中说,数警告被存储为文本......)

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


解决方案

您必须提供的值作为双。作为<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html#setCellValue%28boolean%29\">the医生说:


  

setCellValue


  
  

公共无效setCellValue(double值)


  
  

有关细胞设定数值


  
  

参数


  
  

的 - 数值设置此单元格。对于公式,我们将设置
  precalculated值,Numerics的,我们将其值设置。对于其他类型的
  我们将细胞变成一个数字细胞并将其值设置。


因此​​,应

  dCell.setCellValue(新双(123));

  dCell.setCellValue(123); //记住,你没有的样子,实际上传递一个字符串(不含引号)

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?

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)

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)

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

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