使用Apache POI格式化Excel单元格为美元,其中机器货币不是美元



我正在使用Java和Apache POI来格式化Excel工作表。

我想将单元格格式设置为美元。

使用Apache POI的基本Excel货币格式的建议,我得到的是单元格被格式化为谢克尔(这恰好是我的机器的默认货币)

如何将单元格格式化为美元,即使我的机器的默认货币不是美元。

我使用的代码如下

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

数字7来自http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html

这行得通(这是Gagravarr的建议)

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

最新更新