使用Apache POI在Excel千位分隔符格式号码 [英] Format number with thousands separator in Excel using Apache POI

查看:2905
本文介绍了使用Apache POI在Excel千位分隔符格式号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要格式化一些数量的细胞,用逗号作为千位分隔符。例如:

I want to format some number cells, with a comma as thousands separator. For example:

12        -> 12
1200      -> 1,200
12000     -> 12,000
12000000  -> 12,000,000
120000000 -> 120,000,000

我有以下的code。我应该用什么作为 formatStr ?有一个简单的方法?还是我来检测,以便产生像这样的零的个数#,###,###

I have the following code. What should I use as formatStr? Is there an easy way? Or do I have to detect the number of zeros in order to produce something like this #,###,###?

String formatStr = "";
HSSFCellStyle style = workbook.createCellStyle();
HSSFDataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat(formatStr));
cell.setCellStyle(style);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

请在我处理的数字头脑。细胞类型将是数字,而不是字符串。

Keep in mind that I'm dealing with numbers. The cell type will be numeric, not string.

更新

推荐答案

只是#,### #,## 0 应该是足够了。 Excel中除$ P $点这个具有千位分隔符每三个数字(不只是过去三年,我推断之前您所期望的)。

Just #,### or #,##0 should be sufficient. Excel interprets this as having thousands separators every three digits (not just before the last three, which I infer is what you were expecting).

在一个教人以渔的精神,这是你如何找出自己:

In the spirit of teaching a man to fish, this is how you can find out for yourself:

格式为数字0的小数位,与1000分隔符:

Format as Number, 0 decimal places, with 1000 separator:

点击确定,然后再重新打开数字格式对话框,然后转到自定义。看一看格式化code(类型)。它说#,## 0 ,这对我来说会给出确切的结果相同#,###

Click OK, then re-open the number format dialog and go to Custom. Have a look at the formatting code ("Type"). It says #,##0, which for me gives the exact same result as #,###.

这篇关于使用Apache POI在Excel千位分隔符格式号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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