Apache POI Currency Data Format

☆樱花仙子☆ 提交于 2019-12-06 01:59:49

问题


I try to convert numbers into european currency style with Apache POI

HSSFDataFormat cf = workbook.createDataFormat();
currencyCellStyle = workbook.createCellStyle();
currencyCellStyle.setDataFormat(cf.getFormat("#.###,#0"));

I have for example the number 2400 and 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

currencyCellStyle.setDataFormat(cf.getFormat("#.###,00"));

I get the result 2400,00 and 2,400. Thats also not what I want.

Is there a possibility to get both values correct?

Thx and Greetings


回答1:


Finally Gagravarr gave the right tips to solve this question.

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

 cell.getCellStyle().getDataFormatString() 

The result was #,##0.00\ _€

So I used this format in the upper code snippet, which gave the correct result.

Thx




回答2:


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.




回答3:


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:

#,##0\ "€";\-#,##0\ "€"

So set format:

cell.getCellStyle().setDataFormat(cf.getFormat("#,##0\\ \"€\";\\-#,##0\\ \"€\""));

Hope this will help some out.



来源:https://stackoverflow.com/questions/13838495/apache-poi-currency-data-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!