系统单元格格式预设值:
|
S/No. |
Format | Vlaue | S/No |
Format | Value |
| 1 | General | 0 | 12 | h:mm AM/PM | 0x12 |
| 2 | 0 | 1 | 13 | h:mm:ss AM/PM | 0x13 |
| 3 | 0.00 | 2 | 14 | h:mm | 0x14 |
| 4 | #,##0 | 3 | 15 | h:mm:ss | 0x15 |
| 5 | #,##0.00 | 4 | 16 | m/d/yy h:mm | 0x16 |
| 6 | ($#,##0_);($#,##0) | 5 | 17 | (#,##0_);[Red](#,##0) | 0x26 |
| 7 | ($#,##0_);[Red]($#,##0) | 6 | 18 | (#,##0.00_);(#,##0.00) | 0x27 |
| 8 | ($#,##0.00);($#,##0.00) | 7 | 19 | (#,##0.00_);[Red](#,##0.00) | 0x28 |
| 9 | ($#,##0.00_);[Red]($#,##0.00) | 8 | 20 | _(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_) | 0x29 |
| 10 | 0% | 9 | 21 | _($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_) | 0x2a |
| 11 | 0.00% | 0xa | 22 | _(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_) | 0x2b |
| 12 | 0.00E+00 | 0xb | 23 | _($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_) | 0x2c |
| 13 | # ?/? | 0xc | 24 | mm:ss | 0x2d |
| 14 | # ??/?? | 0xd | 25 | [h]:mm:ss | 0x2e |
| 15 | m/d/yy | 0xe | 26 | mm:ss.0 | 0x2f |
| 16 | d-mmm-yy | 0xf | 27 | ##0.0E+0 | 0x30 |
| 17 | d-mmm | 0x10 | 28 | @-This is text format. | 0x31 |
| 18 | mmm-yy | 0x11 | 29 | text-Alias for "@" | 0x31 |
//创建数字单元格样式
IFont numFont = hSSFWorkbook.CreateFont();
numFont.FontName = "宋体";
numFont.FontHeightInPoints = 10;
numStyle = hSSFWorkbook.CreateCellStyle();
numStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
numStyle.BottomBorderColor = HSSFColor.Black.Index;
numStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
numStyle.LeftBorderColor = HSSFColor.Black.Index;
numStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
numStyle.RightBorderColor = HSSFColor.Black.Index;
numStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
numStyle.TopBorderColor = HSSFColor.Black.Index;
numStyle.Alignment = HorizontalAlignment.Right;
numStyle.SetFont(numFont);
//货币千分位保留两位小数
numStyle.DataFormat = 4;
//numStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
最初的时候没有看懂numStyle.DataFormat = 4是啥意思,查阅了一些资料后才发现,很多写法如下:
numStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
NLogHelper.WriteLog("numStyle格式", "¥#,##0 -> " + HSSFDataFormat.GetBuiltinFormat("¥#,##0"), LogLevel.Warn);
NLogHelper.WriteLog("numStyle格式", "#,##0 -> " + HSSFDataFormat.GetBuiltinFormat("#,##0"), LogLevel.Warn);
NLogHelper.WriteLog("numStyle格式", "#,##0.00 -> " + HSSFDataFormat.GetBuiltinFormat("#,##0.00"), LogLevel.Warn);
NLogHelper.WriteLog("numStyle格式", "$#,##0.00 -> " + HSSFDataFormat.GetBuiltinFormat("$#,##0.00"), LogLevel.Warn);
NLogHelper.WriteLog("numStyle格式", "(#,##0.00_);(#,##0.00) -> " + HSSFDataFormat.GetBuiltinFormat("(#,##0.00_);(#,##0.00)"), LogLevel.Warn);
输出结果:
2021-07-26 15:06:49.8224 numStyle格式 ¥#,##0 -> -1
2021-07-26 15:06:49.8224 numStyle格式 #,##0 -> 3
2021-07-26 15:06:49.8224 numStyle格式 #,##0.00 -> 4
2021-07-26 15:06:49.8224 numStyle格式 $#,##0.00 -> -1
2021-07-26 15:06:49.8224 numStyle格式 (#,##0.00_);(#,##0.00) -> -1
后来通过打印日志,才发现,原来这个的返回值正是4,同直接设置 numStyle.DataFormat 的值 = 4效果一样,如果获取不到对应的格式,返回-1。在结合官方文档中各种预设好的格式的值,最后才明白后边value的含义。直接设置值有一个好处,就是不会出现取不到格式值的情况,如果设置为-1,导出的时候就会报错。但是直接设置,往往让不知道的小伙伴看到之后非常迷茫,不知道这个4是干啥的,所以最好注释一下。
有了对应的格式,设置值的时候也要注意下,不要设置成文本格式,传参的时候用double。
double val = 0;
double.TryParse(item[Convert.ToString(dr["field"])].ToString(), out val);
rowContent.CreateCell(k).SetCellValue(val);
rowContent.GetCell(k).CellStyle = numStyle;
导出效果:

C#使用NPOI设置Excel数值格式为货币
本文介绍了在C#中利用NPOI库导出Excel时,如何设置单元格格式为货币类型,包括理解预设格式的数字代码以及避免设置为文本格式的问题,以确保数据正确导出。
1万+





