java setCellType(HSSFCELL.CELL_TYPE_NUMERIC) 在 apache poi 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27840258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setCellType(HSSFCELL.CELL_TYPE_NUMERIC) is not working in apache poi
提问by Nishith Shah
SetCellType(HSSFCELL.CELL_TYPE_NUMERIC)
is not working in apache poi.
Anyone have used this feature of POI in JAVA?
SetCellType(HSSFCELL.CELL_TYPE_NUMERIC)
不在 apache poi 中工作。有人在JAVA中使用过POI的这个功能吗?
When I created XLS file using POI and cell contains integer value and I set cell type as numeric at that time that cell reflects some other integer value.
Example:
Using JAVA program and poi utility, I am putting in Cell A1
value "5"
. Now this cell contains Integer value but by default cell type is CELL_TYPE_STRING
. So this cell gives me error and asked me to convert this cell to Number.
But using program when I will set this cell type as CELL_TYPE_NUMERIC
at that time it does not give me error in XLS file but it shows me different value.
当我使用 POI 创建 XLS 文件并且单元格包含整数值并且我将单元格类型设置为数字时,该单元格反映了其他一些整数值。示例:使用 JAVA 程序和 poi 实用程序,我正在输入Cell A1
value "5"
。现在此单元格包含整数值,但默认情况下单元格类型为CELL_TYPE_STRING
. 所以这个单元格给了我错误,并要求我将此单元格转换为数字。但是当我将设置此单元格类型时使用程序,因为CELL_TYPE_NUMERIC
它不会在 XLS 文件中给我错误,但它会显示不同的值。
Anyone has faced such issue? If yes then is there any solution for this. I stuck up with this one.
有人遇到过这样的问题吗?如果是,那么是否有任何解决方案。我坚持了这个。
Please ask me if you need some more clarification.
如果您需要更多说明,请询问我。
Thanks in advance.
提前致谢。
回答by Akshay
Since you are using XSSF
not HSSF
, use cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC)
instead of setCellType(HSSFCELL.CELL_TYPE_NUMERIC)
.
由于您使用的是XSSF
not HSSF
,请使用cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC)
代替setCellType(HSSFCELL.CELL_TYPE_NUMERIC)
。
回答by Uday kumar
** Give a Integer value instead of a String Value.**
** 给出整数值而不是字符串值。**
HSSFCell cell = row.createCell(col);
cell.setCellStyle(style);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
if(value!=null)
cell.setCellValue(Integer.parseInt(value));