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

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

问题描述

我能够将单元格设置为日期格式,但我一直无法将单元格设置为货币格式...任何人都有如何创建样式以使其工作的示例?我下面的代码显示了我正在创建的样式...... styleDateFormat 像冠军一样工作,而 styleCurrencyFormat 对单元格没有影响.

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)"

以下是更多示例:http://www.roseindia.net/java/poi/setDataFormat.shtml

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

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