数字格式使指能够控制excel单元格中数字如何显示的格式字符串。例如,我们可以对数字12345应用数字格式“0.00”,使之显示为12345.00。数字格式字符串通常由以下某个或某几个说明符构成:
- # :数字占位符
- 0 :0占位符
- . :小数点
- , :分组符
- % :百分号占位符
- [red] :颜色说明符
更多格式说明符,请参阅:
c#
//初始化workbook对象
workbook workbook = new workbook();
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//写入文本到b1,c1
sheet.range["b1"].text = "数字格式";
sheet.range["c1"].text = "数字±12345678应用格式后的效果";
sheet.range["b1"].style.font.isbold = true;
sheet.range["c1"].style.font.isbold = true;
//在b3中写入文本(即数字格式),在c3中写数字并应用数字格式
sheet.range["b3"].text = "0";
sheet.range["c3"].numbervalue = 12345678;
sheet.range["c3"].numberformat = "0";
//重复上面的步骤写入更多格式及对应效果
sheet.range["b4"].text = "0.00";
sheet.range["c4"].numbervalue = 12345678;
sheet.range["c4"].numberformat = "0.00";
sheet.range["b5"].text = "#,##0.00";
sheet.range["c5"].numbervalue = 12345678;
sheet.range["c5"].numberformat = "#,##0.00";
sheet.range["b6"].text = "¥#,##0.00";
sheet.range["c6"].numbervalue = 12345678;
sheet.range["c6"].numberformat = "¥#,##0.00";
sheet.range["b7"].text = "0;[red]-0";
sheet.range["c7"].numbervalue = -12345678;
sheet.range["c7"].numberformat = "0;[red]-0";
sheet.range["b8"].text = "0.00;[red]-0.00";
sheet.range["c8"].numbervalue = -12345678;
sheet.range["c8"].numberformat = "0.00;[red]-0.00";
sheet.range["b9"].text = "#,##0;[red]-#,##0";
sheet.range["c9"].numbervalue = -12345678;
sheet.range["c9"].numberformat = "#,##0;[red]-#,##0";
sheet.range["b10"].text = "#,##0.00;[red]-#,##0.000";
sheet.range["c10"].numbervalue = -12345678;
sheet.range["c10"].numberformat = "#,##0.00;[red]-#,##0.00";
sheet.range["b11"].text = "0.00e 00";
sheet.range["c11"].numbervalue = 12345678;
sheet.range["c11"].numberformat = "0.00e 00";
sheet.range["b12"].text = "0.00%";
sheet.range["c12"].numbervalue = 12345678;
sheet.range["c12"].numberformat = "0.00%";
//设置[b3:b12]、[c3:c12]背景颜色
sheet.range["b3:b12"].style.knowncolor = excelcolors.gray25percent;
sheet.range["c3:c12"].style.knowncolor = excelcolors.gray50percent;
//设置2、3列列宽
sheet.columns[1].columnwidth = 25;
sheet.columns[2].columnwidth = 30;
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'初始化workbook对象
dim workbook as new workbook()
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'写入文本到b1,c1
sheet.range("b1").text = "数字格式"
sheet.range("c1").text = "数字±12345678应用格式后的效果"
sheet.range("b1").style.font.isbold = true
sheet.range("c1").style.font.isbold = true
'在b3中写入文本(即数字格式),在c3中写数字并应用数字格式
sheet.range("b3").text = "0"
sheet.range("c3").numbervalue = 12345678
sheet.range("c3").numberformat = "0"
'重复上面的步骤写入更多格式及对应效果
sheet.range("b4").text = "0.00"
sheet.range("c4").numbervalue = 12345678
sheet.range("c4").numberformat = "0.00"
sheet.range("b5").text = "#,##0.00"
sheet.range("c5").numbervalue = 12345678
sheet.range("c5").numberformat = "#,##0.00"
sheet.range("b6").text = "¥#,##0.00"
sheet.range("c6").numbervalue = 12345678
sheet.range("c6").numberformat = "¥#,##0.00"
sheet.range("b7").text = "0;[red]-0"
sheet.range("c7").numbervalue = -12345678
sheet.range("c7").numberformat = "0;[red]-0"
sheet.range("b8").text = "0.00;[red]-0.00"
sheet.range("c8").numbervalue = -12345678
sheet.range("c8").numberformat = "0.00;[red]-0.00"
sheet.range("b9").text = "#,##0;[red]-#,##0"
sheet.range("c9").numbervalue = -12345678
sheet.range("c9").numberformat = "#,##0;[red]-#,##0"
sheet.range("b10").text = "#,##0.00;[red]-#,##0.000"
sheet.range("c10").numbervalue = -12345678
sheet.range("c10").numberformat = "#,##0.00;[red]-#,##0.00"
sheet.range("b11").text = "0.00e 00"
sheet.range("c11").numbervalue = 12345678
sheet.range("c11").numberformat = "0.00e 00"
sheet.range("b12").text = "0.00%"
sheet.range("c12").numbervalue = 12345678
sheet.range("c12").numberformat = "0.00%"
'设置[b3:b12]、[c3:c12]背景颜色
sheet.range("b3:b12").style.knowncolor = excelcolors.gray25percent
sheet.range("c3:c12").style.knowncolor = excelcolors.gray50percent
'设置2、3列列宽
sheet.columns(1).columnwidth = 25
sheet.columns(2).columnwidth = 30
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)