java Apache POI 货币数据格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13838495/
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
Apache POI Currency Data Format
提问by Logarith
I try to convert numbers into european currency style with Apache POI
我尝试使用 Apache POI 将数字转换为欧洲货币样式
HSSFDataFormat cf = workbook.createDataFormat();
currencyCellStyle = workbook.createCellStyle();
currencyCellStyle.setDataFormat(cf.getFormat("#.###,#0"));
I have for example the number 2400 and 2.4
例如,我有数字 2400 和 2.4
What I want is 2400,00 and 2,40 . But POI gives me 2400,0 and 2,40. When I try to change it to
我想要的是 2400,00 和 2,40 。但是 POI 给了我 2400,0 和 2,40。当我尝试将其更改为
currencyCellStyle.setDataFormat(cf.getFormat("#.###,00"));
I get the result 2400,00 and 2,400. Thats also not what I want.
我得到的结果是 2400,00 和 2,400。那也不是我想要的。
Is there a possibility to get both values correct?
是否有可能使两个值都正确?
Thx and Greetings
谢谢和问候
回答by Logarith
Finally Gagravarr gave the right tips to solve this question.
最后 Gagravarr 给出了解决这个问题的正确提示。
The final solution is:
最终的解决方案是:
HSSFDataFormat cf = workbook.createDataFormat();
currencyCellStyle = workbook.createCellStyle();
currencyCellStyle.setDataFormat(cf.getFormat("#,##0.00\ _"));
The solution came up after creating an excel file manually. Then read it in by Apache Poi and extracting the format string with
手动创建excel文件后出现了解决方案。然后由 Apache Poi 读入并提取格式字符串
cell.getCellStyle().getDataFormatString()
The result was #,##0.00\ _
结果是#,##0.00\_
So I used this format in the upper code snippet, which gave the correct result.
所以我在上面的代码片段中使用了这种格式,它给出了正确的结果。
Thx
谢谢
回答by NevinJ
An update to the above approach: After manually creating excel file with the required currency formatting, the format string can be obtained under the "Custom" category (in Format Cell dialog). That way we need not read the excel via poi to get this string.
上述方法的更新:手动创建具有所需货币格式的excel文件后,可以在“自定义”类别下(在格式单元格对话框中)获取格式字符串。这样我们就不需要通过 poi 读取 excel 来获取这个字符串。
回答by Philipp Zitzmann
Cannot write a Comment due my reputation. But if you need Integer with dots each 3rd Number (e.g. 24.000.123 ) the Pattern is:
由于我的声誉,无法写评论。但是,如果您需要 Integer 与每个第三个数字(例如 24.000.123 )的点,则模式为:
#,##0\ "";\-#,##0\ ""
So set format:
所以设置格式:
cell.getCellStyle().setDataFormat(cf.getFormat("#,##0\ \"\";\-#,##0\ \"\""));
Hope this will help some out.
希望这会有所帮助。