与Apache POI的基本Excel的货币格式 [英] Basic Excel currency format with Apache POI

查看:919
本文介绍了与Apache POI的基本Excel的货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够获得细胞的日期格式,但我一直无法得到的细胞作为货币格式...任何人都有如何创建一种风格得到这个工作的例子吗?下面我的code显示我创建的......工作styleDateFormat像一个冠军,而styleCurrencyFormat还没有对电池影响的样式。

 私人HSSFWorkbook WB;
私人HSSFCellStyle styleDateFormat = NULL;
私人HSSFCellStyle styleCurrencyFormat = NULL;

......

 公共CouponicsReportBean(){
WB =新HSSFWorkbook();
InitializeFonts();}公共无效InitializeFonts()
{
styleDateFormat = wb.createCellStyle();
styleDateFormat.setDataFormat(HSSFDataFormat.getBuiltinFormat(M / D / YY));
    styleCurrencyFormat = wb.createCellStyle();
    styleCurrencyFormat.setDataFormat(HSSFDataFormat.getBuiltinFormat($#,## 0.00));}


解决方案

通过文档后挖多一点,我找到了答案:

<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDataFormat.html\">http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDataFormat.html

只需要找到一个合适的pre-设置格式,并提供code。

  styleCurrencyFormat.setDataFormat((短)8); // 8 =($#,## 0.00 _); [红色]($#,## 0.00)

下面是更多的例子:
<一href=\"http://www.roseindia.net/java/poi/setDataFormat.shtml\">http://www.roseindia.net/java/poi/setDataFormat.shtml

I'm able to get cells to format as Dates, but I've been unable to get cells to format as currency... Anyone have an example of how to create a style to get this to work? My code below show the styles I'm creating... the styleDateFormat works like a champ while styleCurrencyFormat has no affect on the cell.

private HSSFWorkbook wb;
private HSSFCellStyle styleDateFormat = null;
private HSSFCellStyle styleCurrencyFormat = null;

......

public CouponicsReportBean(){
	wb = new HSSFWorkbook();
	InitializeFonts();

}

public void InitializeFonts()
{
	styleDateFormat = wb.createCellStyle();
	styleDateFormat.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));


    styleCurrencyFormat = wb.createCellStyle();
    styleCurrencyFormat.setDataFormat(HSSFDataFormat.getBuiltinFormat("$#,##0.00"));

}

解决方案

After digging through the documentation a bit more, I found the answer:

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDataFormat.html

Just need to find an appropriate pre-set format and supply the code.

	styleCurrencyFormat.setDataFormat((short)8); //8 = "($#,##0.00_);[Red]($#,##0.00)"

Here are more examples: http://www.roseindia.net/java/poi/setDataFormat.shtml

这篇关于与Apache POI的基本Excel的货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆