翻譯|使用教程|編輯:胡濤|2023-01-04 10:34:22.637|閱讀 238 次
概述:本指南介紹了一種使用數據創建 Word 表格并通過 Spire.Doc for .NET 在 C#.NET 中設置其邊框的解決方案
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
如果您正在處理提案請求、標準操作程序文件或規定格式的其他文檔類型,則 Word 文檔可能是一頁又一頁的純文本。你的文件會很麻煩而且不清楚。所以你的文件需要一個表格來記錄你需要顯示的信息。表格不僅提供了信息的可視化分組,也使編寫者更方便地修改和查詢表格中的數據。特別是當您的表格有豐富多彩的邊框時,您的文檔會令人驚嘆。
Spire.Doc for .NET,專業的.NET Word組件,操作Word文檔,提供了一個Table類來對表格執行任務,例如,包括行、列、單元格、邊框和表格布局操作。本指南介紹了一種使用數據創建 Word 表格并通過 Spire.Doc for .NET 在 C#.NET 中設置其邊框的解決方案。要完成本指南,您應該首先下載并安裝 Spire.Doc for .NET。
下面將通過一個具體的例子來逐步創建Word表格并設置其邊框。下面的屏幕截圖顯示了此示例的結果。
我們假設您在本示例之前已經完成教程 Spire.Doc 快速入門。
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Table { Public class table { static void Main(string[] args) { //Open a blank word document as template Document document = new Document(Blank.doc"); addTable(document.Sections[0]); //Save doc file. document.SaveToFile("Sample.doc",FileFormat.Doc); //Launching the MS Word file. System.Diagnostics.Process.Start ("Sample.doc"); } private void addTable(Section section) { //create a table with border Spire.Doc.Table table = section.AddTable(true); String[] header = { "Name", "Capital", "Continent", "Area", "Population" }; String[][] data = { new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"}, new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"}, new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"}, new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"}, new String[]{"Peru", "Lima", "South America", "1285215", "21600000"}, new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"}, }; table.ResetCells(data.Length + 1, header.Length); // ***************** First Row ************************* TableRow Frow = table.Rows[0]; Frow.IsHeader = true; Frow.Height = 20; //unit: point, 1point = 0.3528 mm Frow.HeightType = TableRowHeightType.Exactly; Frow.RowFormat.BackColor = Color.Pink; for (int i = 0; i < header.Length; i++) { Frow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p = Frow.Cells[i].AddParagraph(); p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left; TextRange txtRange = p.AppendText(header[i]); txtRange.CharacterFormat.Bold = true; } for (int r = 0; r < data.Length; r++) { TableRow dataRow = table.Rows[r + 1]; dataRow.Height = 20; dataRow.HeightType = TableRowHeightType.Exactly; dataRow.RowFormat.BackColor = Color.Empty; for (int c = 0; c < data[r].Length; c++) { dataRow.Cells[c].CellFormat.VerticalAlignment =VerticalAlignment.Middle; dataRow.Cells[c].AddParagraph().AppendText(data[r][c]); } } //set right border of table table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Right. LineWidth = 2.0F; table.TableFormat.Borders.Right.Color = Color.GreenYellow; //set top border of table table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Top.LineWidth = 4.0F; table.TableFormat.Borders.Top.Color = Color.GreenYellow; //set left border of table table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Left.LineWidth = 2.0F; table.TableFormat.Borders.Left.Color = Color.GreenYellow; //set bottom border is none table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None; //set vertical and horizontal border table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot; table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.None; table.TableFormat.Borders.Vertical.Color = Color.Orange; } } }
以上便是如何在C#、VB.NET中設置Word表格樣式,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn