Apache POI Currency Formating
Hi All,
I used the apache POI for generating excel sheet.
I have a column that should contain the price of the product.
How can I format the column to Currency type using HSSFDataFormat and also display euro symbol? Please give an example line.
Please help me.
Garollous
I'd recommend using JExcel rather than POI. I think it's a better library.%
> I'd recommend using JExcel rather than POI. I think> it's a better library.> > %because?
We are using Apache POI for last 2 years. This is a new requirement and cannot change the design.Please advice.
aniseed,Do you have any idea how to solve this.
> How can I format the column to Currency type using
> HSSFDataFormat and also display euro symbol? Please
> give an example line.
HSSFDataFormat.getFormat(7) is the one which has currency symbol and displays the "currency like" value. You need to set the format ( setDataFormat() ) on the HSSFCellStyle object of the particular cell [HSSFCell]
> aniseed,> > Do you have any idea how to solve this.Yes and http://jakarta.apache.org/poi/hssf/quick-guide.html always comes handy.
Thanks for your prompt reply.
HSSFCellStyle special = workbook.createCellStyle();
HSSFDataFormat format = workbook.createDataFormat();
special.setDataFormat(format.getFormat(7));
This gives Java error that getFormat can only accept String and not int.
I am using 2.0 version of POI, should I upgrade?
> This gives Java error that getFormat can only accept
> String and not int.
Then use the String mentioned in the documentation for POI.
"($#,##0.00);($#,##0.00)"
> I am using 2.0 version of POI, should I upgrade?
You might, but you haven't yet stumbled upon something which you cannot solve. In POI 2.5, there were several features added which could create a better Excel workbook. If you need some of those features, you might consider upgrading.
It's not a big deal to upgrade anyway. You need to only swap a JAR, but I should warn you that some of the API was changed incompatibly and you might need to modify your code and recompile it again to make it work after the upgrade. In any case, I would suggest that you search for the changelog of the latest version and check.
Thanks a ton!! But I have my last question.. How do I display Euro symbol for currency?Your option worked perfect and showed the $ symbol. Please help me for this last time.
> But I have my last question..
> How do I display Euro symbol for currency?
> Your option worked perfect and showed the $ symbol.
The symbol for the currency would depend on the OS locale (that's the way Excel works). If your locale is set to point to somewhere in Europe, I suppose it would show up Euro instead of the Dollar.
Okay, I verified it as well. I changed from the Control Panel > Regional and Language Options to English UK and it started showing the Pound instead of the Dollar.
I hope that answers your question.
I tried changing the settings, but nothing changed.Everything looked the same with $ symbol. Any Ideas to show Euro?
> I tried changing the settings, but nothing> changed.Everything looked the same with $ symbol. Any> Ideas to show Euro?Did you try it on Excel after formatting the cell? It's a simple cell formatting and it works for me.
The code for the euro symbol is "\u20AC", you might try using that in your formatter explicitly:setDataFormat(format.getFormat("(\u20AC#,##0.00);(\u20AC#,##0.00)")); If that doesn't work, I have no clue myself.
cafala at 2007-7-14 23:28:09 >

Hi Cafal,The data is displayed as (?9,999.00) when I used your specification. Is it related to any encoding.Thanks
I also specified cell.setEncoding(HSSFCell.ENCODING_UTF_16);But the result is the same.
Well, I made the suggestion based on the code page without trying anything in the hopes that it would help. Obviously, it didn't.
So I did a little more looking around, and Microsoft has inserted the euro at index 128 in their 1252 codeset (\u0080). I can't get that to display correctly either.
The only alternative I have left, and which I don't have time for this afternoon, is to create a worksheet with one cell formatted with a euro, read it in with POI, and print out the resulting format. That will definitely give you *A* format option, but I don't know if it will translat back into a new page from POI.
cafala at 2007-7-21 11:00:08 >

> > I'd recommend using JExcel rather than POI. I
> think
> > it's a better library.
> >
> > %
>
> because?
(1) I've tried both and I think JExcel is superior.
(2) It appears that POI isn't active anymore. Andy Khan is still working on JExcel, as far as I can tell.
%
After a single test, I have to say I now prefer the JExcel API as well. The following code to have a euro sign appear worked on the first try:
WritableWorkbook workbook =
Workbook.createWorkbook(new File("output.xls"));
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
jxl.write.NumberFormat euros =
new jxl.write.NumberFormat("\u20AC#,##0.00");
WritableCellFormat eurosFormat = new WritableCellFormat(euros);
jxl.write.Number numberCell =
new jxl.write.Number(0, 0, 123.33, eurosFormat);
sheet.addCell(numberCell);
workbook.write();
workbook.close();
Output - cell(0,0): 123.33
As you can see, the code accepted the standard value for a euro sign (\u20AC) without any problems.
cafala at 2007-7-21 11:00:08 >

Thanks Cafal. I will try it now and get back to you.
Cafal,
I appreciate your patience with me, but in Java if you format Euro by Locale the symbol appears after the number and not before the number.
Example: 4 052,60
But using Jexcel approach I specified
" jxl.write.NumberFormat("#,##0.00 \u20AC"); " did not give me the Euro symbol . I just gave the number.
Please help on this and I will close this issue.
Cafal,
I appreciate your patience with me, but in Java if you format Euro by Locale the symbol appears after the number and not before the number.
Example: 4 052,60
But using Jexcel approach I specified
" jxl.write.NumberFormat("#,##0.00 \u20AC"); " did not give me the Euro symbol . It just gave the number.
Please help on this and I will close this issue.
To get it to append the euro symbol I had to tell it not to validate the pattern:jxl.write.NumberFormat euros =
new jxl.write.NumberFormat("#,#00.00 \u20AC", jxl.write.NumberFormat.COMPLEX_FORMAT);
That worked fine for me.
cafala at 2007-7-21 11:00:08 >

I am able to display the euro symbol now but not the formatting. I am still having trouble with that Euro formats. The number format should be 123,33 and not 123.33 Please help.
That I can't help you with - I don't know how to tell Excel itself to use a comma for the radix point, much less through JExcel.Just FYI - I did do the experiment of reading in a formatted cell with POI, and it gave me a style index of 164, which I am unable to use in POI.
cafala at 2007-7-21 11:00:08 >

Cafal,
I am sorry not to get back for so long. Ok Fine, but using your suggestion I made code changes and it works fine for US offices.
I asked my manager for a french laptop to see if it works just fine in that laptop for euro. I will let you know.
But, I did not know how to apply font/ background color for the cell.
This is my code...
jxl.write.Number numberCell = new jxl.write.Number(col, row, number, acctFormat);
wsheet.addCell(numberCell);
How do I add the font properties , back ground color to this cell?
Please help me.