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

时间:2022-04-13 15:59:03

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

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

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)

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

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

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

1 个解决方案

#1


4  

This works (this is Gagravarr's suggestion)

这是有效的(这是G​​agravarr的建议)

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

#1


4  

This works (this is Gagravarr's suggestion)

这是有效的(这是G​​agravarr的建议)

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