如何将货币类型设置为 POI 中的列 [英] How to set currency type to a column in POI

查看:39
本文介绍了如何将货币类型设置为 POI 中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 java 中的 poi 将 XSSFWorkbook 中的列转换为货币类型?

How do I convert a column to currency type in XSSFWorkbook using poi in java ?

这是我使用的代码:

DataFormat datafrmt = wbAll.createDataFormat();
cellStyle.setDataFormat(datafrmt.getFormat("$#,##0.00")); 

但在设置数据格式后,我尝试打开 Excel 工作表并检查它显示自定义类型的列的格式,但我需要该列是货币类型.我怎样才能做到这一点?

But after setting the data format I tried opening the Excel sheet and checked the format of the column it shows custom type but I need the column to be a currency type. How can I achieve this?

推荐答案

我建议您使用 BuiltinFormats 用于货币.这个似乎就是你要找的那个7, "$#,##0.00);($#,##0.00)".

I would suggest you use one of the BuiltinFormats for currency. This one seems to be the one you're looking for 7, "$#,##0.00);($#,##0.00)".

检查如何Apache 建议通过在他们的演示中制作 XSSFCellStyleMap 来实现.读完后,我认为您只是缺少这行代码才能使格式化工作.

Check how Apache suggests doing it by making a Map of XSSFCellStyle in their demo. After reading it, I think you are only missing this line of code for you format to work.

XSSFCellStyle style3 = wb.createCellStyle();
style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);  // THIS LINE HERE!
style3.setDataFormat(fmt.getFormat("$#,##0.00"));
styles.put("currency", style3);`

要使用 BuiltinFormats,而不是使用 XSSFDataFormat 实例,您可以直接设置所需格式的short,如下所示 style3.setDataFormat((short) 7);

To use the BuiltinFormats, instead of using a XSSFDataFormat instance, you directly set the short of the wanted format, like this style3.setDataFormat((short) 7);

这篇关于如何将货币类型设置为 POI 中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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