NPOI 设置EXCEL单元格格式为 数值

NPOI 设置EXCEL单元格格式为 数值_第1张图片

NPOI设置EXCEL单元格格式为数值,网上有很多信息,但并没有实际解决问题的方法。

实际上EXCEL的格式设置很简单,只要将样式对象的DataFormat属性设置为194即可,代码如下:

#region 普通样式-靠右

// 设置边框
ICellStyle csRight = wb.CreateCellStyle();
csRight.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
csRight.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
csRight.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
csRight.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
csRight.WrapText = true;// 指定单元格自动换行
csRight.SetFont(font);//设置字体
csRight.VerticalAlignment = VerticalAlignment.CENTER;//垂直居中
csRight.Alignment = HorizontalAlignment.RIGHT;
csRight.DataFormat = 194;//HSSFDataFormat.GetBuiltinFormat("###0.00");//(short)CellType.NUMERIC;
#endregion


还有一点要注意,在单元格赋值时要用double类型赋值。

cell = sheet.GetRow(curRowIndex).CreateCell(stub + products.Count * 9 + 1);
                            cell.SetCellValue((double)eamt);
                            cell.CellStyle = csRight;

你可能感兴趣的:(个人日志)