Java – Formatting Excel cell as Dollar with Apache POI where the machine currency is not dollar

apache-poiexceljava

I am using Java and Apache POI to format an Excel worksheet.

I want to format a cell as dollars.

Using the suggestion in Basic Excel currency format with Apache POI, what I get is that the cell is formatted as shekels (which happens to be the default currency of my machine)

How do I format the cell as dollars, even if the default currency of my machine is not dollars.

The code I am using is as follows

CellStyle dollarStyle=wb.createCellStyle();
dollarStyle.setDataFormat(7);
CellUtil.getCell(myRow, 1).setCellStyle(dollarStyle);

The number 7 comes from http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html

Best Answer

This works (this is Gagravarr's suggestion)

CellStyle dollarStyle=wb.createCellStyle();
DataFormat df = wb.createDataFormat();
dollarStyle.setDataFormat(df.getFormat("$#,#0.00"));
Related Topic