Spire.XLS如何对Excel中的单元格进行数字格式化

作者:控件中国网   出处:控件中国网   2016-10-18 14:17:32   阅读:9

数字格式化是一种使用数字格式化字符串来控制数字显示格式的方法,开发人员可以使用格式化字符串'0.00'来格式数字1234.5678为1234.57,一般情况下数字格式化字符串是由一个或多个自定义的数字列表所构成,如下:
  • "#" - Digit placeholder
  • "0" - Zero placeholder
  • "," - Decimal point
  • "." - Decimal separator
  • "[Red]" - Color specifier
  • "%" - Percentage placeholder
Spire.XLS是一款类似Excel软件的无图形界面控件,可以使用API来对Excel进行各种操作处理和转换,Excel可以实现的功能,Spire.XLS也可以实现大多数功能,下面的代码讲述了如何使用Spire.XLS来对Excel中的数字单元格进行各种格式化:
Set_Number_Format.png
/initialize workbook and worksheet
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
 
sheet.Range["B1"].Text = "NUMBER FORMATTING";
sheet.Range["B1"].Style.Font.IsBold = true;
 
//input value for a call and set the number format
sheet.Range["B3"].Text = "0";
sheet.Range["C3"].NumberValue = 1234.5678;
sheet.Range["C3"].NumberFormat = "0";
 
sheet.Range["B4"].Text = "0.00";
sheet.Range["C4"].NumberValue = 1234.5678;
sheet.Range["C4"].NumberFormat = "0.00";
 
sheet.Range["B5"].Text = "#,##0.00";
sheet.Range["C5"].NumberValue = 1234.5678;
sheet.Range["C5"].NumberFormat = "#,##0.00";
 
sheet.Range["B6"].Text = "$#,##0.00";
sheet.Range["C6"].NumberValue = 1234.5678;
sheet.Range["C6"].NumberFormat = "$#,##0.00";
 
sheet.Range["B7"].Text = "0;[Red]-0";
sheet.Range["C7"].NumberValue = -1234.5678;
sheet.Range["C7"].NumberFormat = "0;[Red]-0";
 
sheet.Range["B8"].Text = "0.00;[Red]-0.00";
sheet.Range["C8"].NumberValue = -1234.5678;
sheet.Range["C8"].NumberFormat = "0.00;[Red]-0.00";
 
sheet.Range["B9"].Text = "#,##0;[Red]-#,##0";
sheet.Range["C9"].NumberValue = -1234.5678;
sheet.Range["C9"].NumberFormat = "#,##0;[Red]-#,##0";
 
sheet.Range["B10"].Text = "#,##0.00;[Red]-#,##0.000";
sheet.Range["C10"].NumberValue = -1234.5678;
sheet.Range["C10"].NumberFormat = "#,##0.00;[Red]-#,##0.00";
 
sheet.Range["B11"].Text = "0.00E+00";
sheet.Range["C11"].NumberValue = 1234.5678;
sheet.Range["C11"].NumberFormat = "0.00E+00";
 
sheet.Range["B12"].Text = "0.00%";
sheet.Range["C12"].NumberValue = 1234.5678;
sheet.Range["C12"].NumberFormat = "0.00%";
 
//change background color and set column width to autofit
sheet.Range["B3:B12"].Style.KnownColor = ExcelColors.Gray25Percent;
sheet.AutoFitColumn(2);
sheet.AutoFitColumn(3);
 
//save and launch the file
workbook.SaveToFile("Sample.xls");
System.Diagnostics.Process.Start("Sample.xls");
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat