在 Apache POI HSSF 中,单元类型仍然显示为“常规” Excel,尽管它是数字格式的
我正在使用 Apache POI HSSF 从我的 Java Web 应用程序生成 Excel 电子表格。
我需要一个格式为 “数字” 并带有 2 个小数点的单元格。 (我在 Java 中的值是 BigDecimals,但我可以将它们转换为双精度数,没问题。)我正在使用此代码:
CellStyle numericStyle = wb.createCellStyle();
numericStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
// output in this new style...
row.createCell(cellnum).setCellValue(((BigDecimal)value).doubleValue());
row.getCell(cellnum).setCellStyle(numericStyle);
问题是,即使这有效,Excel 仍然将我的单元格显示为常规 。它们需要显示为数字。因此,例如,0 显示为 0,但它应该是 0.00,如果格式正确(数字),就会出现这种情况。
我看到它生成为常规,因为我可以右键单击单元格,然后选择“设置单元格格式”来查看它现在是什么。它需要由 Apache POI HSSF 设置为“Number”。
I am using Apache POI HSSF to generate an Excel spreadsheet from my Java Web app.
I need a cell formatted as "Number" with 2 decimal points. (My values in Java are BigDecimals, but I can convert them to doubles, no problem.) I am using this code:
CellStyle numericStyle = wb.createCellStyle();
numericStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
// output in this new style...
row.createCell(cellnum).setCellValue(((BigDecimal)value).doubleValue());
row.getCell(cellnum).setCellStyle(numericStyle);
The issue is that, even though this works, Excel still shows my cells as General. They need to be shown as Number. As a result, for example, 0 is shown as 0, but it should be 0.00, which would happen if the format was correct (Number).
I see that it gets generated as General because I can right-click on a cell and then choose "Format Cells" to see what it is right now. It needs to be set to "Number" by Apache POI HSSF.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 Apache POI API 看起来您应该能够指定单元格的类型。
Reviewing the Apache POI API it looks like you should be able to specify the cell's type.
我假设您还没有找到问题的答案。
显示两位小数数字的一种方法是设置单元格样式。这允许您自定义小数位数和小数分隔符等。
稍后,当您填充工作簿时,您可以将此单元格样式应用于所需的单元格:
它不会在 Excel 中显示为数字类型。它显示为自定义类型。但从视觉上看应该是相同的,或者至少足够接近。
I'm assuming you haven't yet found an answer for your question.
One way to display numbers with two decimals is to set a cell style. This allows you to customize the number of decimal places and the decimal separator, among other things.
Later on, when you fill your workbook, you apply this cellStyle to the desired cells:
It won't show as numeric type in Excel. It shows as Custom type. But visually it should be the same, or at least close enough.