使用 poi ,如何将单元格类型设置为数字 [英] using poi , How to set the Cell type as number

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

问题描述

我使用的是 poi 3.6

I am using poi 3.6

我能够正确地创建excel.但是当我尝试将单元格类型设置为 number 时,它总是给我一般的单元格类型.

I am able to create the excel properly. But when I trying to set the cell type as number , it always give me cell type as general.

即在新创建的 excel 中,当我右键单击并转到格式单元格时 -> 在那里我总是发现 number 是一个 General.

i.e. In the newly create excel , when I right click and go to format cell ->there I always found number to be a General.

我的代码是这样的

style.setAlignment(CellStyle.ALIGN_RIGHT);
dataCell.setCellValue(Float.parseFloat(value as String);
dataCell.setCellType(Cell.CELL_TYPE_NUMERIC);
dataCell.setCellStyle(style);

你能建议这里缺少什么吗?

Can you please suggest what is missing here ?

推荐答案

你也可以试试这个方法:

You can try this approach too:

HSSFRow row = sheet.createRow(sheet.getLastRowNum());
HSSFCell cell = row.createCell(0);
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
cell.setCellStyle(style);
cell.setCellValue(Float.parseFloat("21.5"));

当然,看看文档和示例关于数据格式.

Of course, take a look at the documentation and examples about data formats.

这篇关于使用 poi ,如何将单元格类型设置为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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