文檔首頁>>Spire.XLS 系列教程>>Spire.XLS數(shù)據(jù)處理系列教程:如何設(shè)置數(shù)字格式和文字對齊方式
Spire.XLS數(shù)據(jù)處理系列教程:如何設(shè)置數(shù)字格式和文字對齊方式
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.XLS for .NET 是一款專業(yè)的 .NET Excel 組件, 它可以用在各種.NET 框架中,包括 .NET Core、ASP.NET 和 Windows Forms 等相關(guān)的.NET 應(yīng)用程序。在Excel表格中單元格也是我們經(jīng)常會使用到的格式,這時我們?nèi)绾问褂肧pire.XLS來完成呢?這就是本系列教程將給大家詳細講解的。
C# 設(shè)置 Excel 中數(shù)字格式
數(shù)字格式使指能夠控制Excel單元格中數(shù)字如何顯示的格式字符串。例如,我們可以對數(shù)字12345應(yīng)用數(shù)字格式“0.00”,使之顯示為12345.00。數(shù)字格式字符串通常由以下某個或某幾個說明符構(gòu)成:
- # :數(shù)字占位符
- 0 :0占位符
- . :小數(shù)點
- , :分組符
- % :百分號占位符
- [Red] :顏色說明符
//初始化Workbook對象 Workbook workbook = new Workbook(); //獲取第一個工作表 Worksheet sheet = workbook.Worksheets[0]; //寫入文本到B1,C1 sheet.Range["B1"].Text = "數(shù)字格式"; sheet.Range["C1"].Text = "數(shù)字±12345678應(yīng)用格式后的效果"; sheet.Range["B1"].Style.Font.IsBold = true; sheet.Range["C1"].Style.Font.IsBold = true; //在B3中寫入文本(即數(shù)字格式),在C3中寫數(shù)字并應(yīng)用數(shù)字格式 sheet.Range["B3"].Text = "0"; sheet.Range["C3"].NumberValue = 12345678; sheet.Range["C3"].NumberFormat = "0"; //重復(fù)上面的步驟寫入更多格式及對應(yīng)效果 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%"; //設(shè)置[B3:B12]、[C3:C12]背景顏色 sheet.Range["B3:B12"].Style.KnownColor = ExcelColors.Gray25Percent; sheet.Range["C3:C12"].Style.KnownColor = ExcelColors.Gray50Percent; //設(shè)置2、3列列寬 sheet.Columns[1].ColumnWidth = 25; sheet.Columns[2].ColumnWidth = 30; //保存文檔 workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013);
C# Excel 中設(shè)置文字對齊方式、方向和換行
在Excel表格中輸入文字時,我們常常需要調(diào)整文字對齊方式或者對文字進行換行。接下來將介紹如何通過編程的方式設(shè)置文字對齊方式,改變文字方向以及對文字進行換行。
//創(chuàng)建Workbook對象 Workbook wookbook = new Workbook(); Worksheet sheet = wookbook.Worksheets[0]; //添加說明文字 sheet.Range["B1"].Text = "文字對齊方式"; sheet.Range["D1"].Text = "文字方向"; sheet.Range["F1"].Text = "文字換行"; sheet.Range["B1:F1"].Style.Font.IsBold = true; //左對齊 sheet.Range["B3"].Text = "左對齊"; sheet.Range["B3"].Style.HorizontalAlignment = HorizontalAlignType.Left; //水平居中 sheet.Range["B4"].Text = "水平居中"; sheet.Range["B4"].Style.HorizontalAlignment = HorizontalAlignType.Center; //右對齊 sheet.Range["B5"].Text = "右對齊"; sheet.Range["B5"].Style.HorizontalAlignment = HorizontalAlignType.Right; //居上 sheet.Range["B7"].Text = "居上"; sheet.Range["B7"].Style.VerticalAlignment = VerticalAlignType.Top; //居中 sheet.Range["B8"].Text = "垂直居中"; sheet.Range["B8"].Style.VerticalAlignment = VerticalAlignType.Center; //居下 sheet.Range["B9"].Text = "居下"; sheet.Range["B9"].Style.VerticalAlignment = VerticalAlignType.Bottom; //分散對齊并居中 sheet.Range["B10"].Text = "水平分散對齊+垂直居中"; sheet.Range["B10"].Style.HorizontalAlignment = HorizontalAlignType.Distributed; sheet.Range["B10"].Style.VerticalAlignment = VerticalAlignType.Center; //逆時針旋轉(zhuǎn)45° sheet.Range["D7"].Text = "旋轉(zhuǎn)45°"; sheet.Range["D7"].Style.Rotation = 45; //逆時針旋轉(zhuǎn)90° sheet.Range["D8"].Text = "旋轉(zhuǎn)90°"; sheet.Range["D8"].Style.Rotation = 90; //順時針旋轉(zhuǎn)45° sheet.Range["D9"].Text = "旋轉(zhuǎn)-45°"; sheet.Range["D9"].Style.Rotation = 135; //順時針旋轉(zhuǎn)90° sheet.Range["D10"].Text = "旋轉(zhuǎn)-90°"; sheet.Range["D10"].Style.Rotation = 180; //添加‘\n’進行文字換行 sheet.Range["F9"].Text = "這是\n手動\n換行"; //自動換行 sheet.Range["F10"].Text = "這是自動換行(額外測試文字)"; sheet.Range["F10"].Style.WrapText = true; //設(shè)置列寬、行高 sheet.Columns[1].ColumnWidth = 15; sheet.Columns[3].ColumnWidth = 15; sheet.Columns[5].ColumnWidth = 15; sheet.Range["B3:B5"].RowHeight = 15; sheet.Range["B7:B10"].RowHeight = 50; //保存文檔 wookbook.SaveToFile("TextAlignment.xlsx", FileFormat.Version2013);
*想要購買Spire.XLS正版授權(quán)的朋友可以聯(lián)系慧都客服哦~