翻譯|使用教程|編輯:胡濤|2023-01-04 10:34:22.637|閱讀 238 次
概述:本指南介紹了一種使用數(shù)據(jù)創(chuàng)建 Word 表格并通過 Spire.Doc for .NET 在 C#.NET 中設(shè)置其邊框的解決方案
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進(jìn)行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
如果您正在處理提案請求、標(biāo)準(zhǔn)操作程序文件或規(guī)定格式的其他文檔類型,則 Word 文檔可能是一頁又一頁的純文本。你的文件會很麻煩而且不清楚。所以你的文件需要一個(gè)表格來記錄你需要顯示的信息。表格不僅提供了信息的可視化分組,也使編寫者更方便地修改和查詢表格中的數(shù)據(jù)。特別是當(dāng)您的表格有豐富多彩的邊框時(shí),您的文檔會令人驚嘆。
Spire.Doc for .NET,專業(yè)的.NET Word組件,操作Word文檔,提供了一個(gè)Table類來對表格執(zhí)行任務(wù),例如,包括行、列、單元格、邊框和表格布局操作。本指南介紹了一種使用數(shù)據(jù)創(chuàng)建 Word 表格并通過 Spire.Doc for .NET 在 C#.NET 中設(shè)置其邊框的解決方案。要完成本指南,您應(yīng)該首先下載并安裝 Spire.Doc for .NET。
下面將通過一個(gè)具體的例子來逐步創(chuàng)建Word表格并設(shè)置其邊框。下面的屏幕截圖顯示了此示例的結(jié)果。
我們假設(shè)您在本示例之前已經(jīng)完成教程 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中設(shè)置Word表格樣式,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn