翻譯|使用教程|編輯:胡濤|2022-12-15 11:13:01.797|閱讀 204 次
概述:在本文中,您將了解如何使用Spire.Doc for .NET以編程方式在 Word 文檔中創(chuàng)建包含數(shù)據(jù)的表格。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) 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等格式文件處理,小巧便捷。
在 MS Word 中,表格可以按行和列組織和呈現(xiàn)數(shù)據(jù),這使得信息更易于理解和分析。在本文中,您將了解如何使用Spire.Doc for .NET以編程方式在 Word 文檔中創(chuàng)建包含數(shù)據(jù)的表格。
首先,您需要添加包含在 Spire.Doc for.NET 包中的 DLL 文件作為您的 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
下面是 Spire.Doc for .NET 提供的一些核心類和方法,用于在 Word 中創(chuàng)建和格式化表格。
姓名 | 描述 |
表類 | 表示 Word 文檔中的表格。 |
TableRow 類 | 代表表格中的一行。 |
TableCell 類 | 代表表格中的特定單元格。 |
Section.AddTbale() 方法 | 將新表添加到指定部分。 |
Table.ResetCells() 方法 | 重置行號(hào)和列號(hào)。 |
Table.Rows 屬性 | 獲取表行。 |
TableRow.Height 屬性 | 設(shè)置指定行的高度。 |
TableRow.Cells 屬性 | 返回單元格集合。 |
TableRow.RowFormat 屬性 | 獲取指定行的格式。 |
詳細(xì)步驟如下
【C#】
using System; using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace WordTable { class Program { static void Main(string[] args) { //Create a Document object Document doc = new Document(); //Add a section Section s = doc.AddSection(); //Define the data for the table String[] Header = { "Date", "Description", "Country", "On Hands", "On Order" }; String[][] data = { new String[]{ "08/07/2021","Dive kayak","United States","24","16"}, new String[]{ "08/07/2021","Underwater Diver Vehicle","United States","5","3"}, new String[]{ "08/07/2021","Regulator System","Czech Republic","165","216"}, new String[]{ "08/08/2021","Second Stage Regulator","United States","98","88"}, new String[]{ "08/08/2021","Personal Dive Sonar","United States","46","45"}, new String[]{ "08/09/2021","Compass Console Mount","United States","211","300"}, new String[]{ "08/09/2021","Regulator System","United Kingdom","166","100"}, new String[]{ "08/10/2021","Alternate Inflation Regulator","United Kingdom","47","43"}, }; //Add a table Table table = s.AddTable(true); table.ResetCells(data.Length + 1, Header.Length); //Set the first row as table header TableRow FRow = table.Rows[0]; FRow.IsHeader = true; //Set the height and color of the first row FRow.Height = 23; FRow.RowFormat.BackColor = Color.LightSeaGreen; for (int i = 0; i < Header.Length; i++) { //Set alignment for cells Paragraph p = FRow.Cells[i].AddParagraph(); FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; p.Format.HorizontalAlignment = HorizontalAlignment.Center; //Set data format TextRange TR = p.AppendText(Header[i]); TR.CharacterFormat.FontName = "Calibri"; TR.CharacterFormat.FontSize = 12; TR.CharacterFormat.Bold = true; } //Add data to the rest of rows and set cell format for (int r = 0; r < data.Length; r++) { TableRow DataRow = table.Rows[r + 1]; DataRow.Height = 20; for (int c = 0; c < data[r].Length; c++) { DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p2 = DataRow.Cells[c].AddParagraph(); TextRange TR2 = p2.AppendText(data[r][c]); p2.Format.HorizontalAlignment = HorizontalAlignment.Center; //Set data format TR2.CharacterFormat.FontName = "Calibri"; TR2.CharacterFormat.FontSize = 11; } } //Save the document doc.SaveToFile("WordTable.docx", FileFormat.Docx2013); } } }
【VB.NET】
Imports System Imports System.Drawing Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace WordTable Class Program Private Shared Sub Main(ByVal args() As String) 'Create a Document object Dim doc As Document = New Document 'Add a section Dim s As Section = doc.AddSection 'Define the data for the table Dim Header() As String = New String() {"Date", "Description", "Country", "On Hands", "On Order"} Dim data(,) As String = New String() {New String() {"08/07/2021", "Dive kayak", "United States", "24", "16"}, New String() {"08/07/2021", "Underwater Diver Vehicle", "United States", "5", "3"}, New String() {"08/07/2021", "Regulator System", "Czech Republic", "165", "216"}, New String() {"08/08/2021", "Second Stage Regulator", "United States", "98", "88"}, New String() {"08/08/2021", "Personal Dive Sonar", "United States", "46", "45"}, New String() {"08/09/2021", "Compass Console Mount", "United States", "211", "300"}, New String() {"08/09/2021", "Regulator System", "United Kingdom", "166", "100"}, New String() {"08/10/2021", "Alternate Inflation Regulator", "United Kingdom", "47", "43"}} 'Add a table Dim table As Table = s.AddTable(true) table.ResetCells((data.Length + 1), Header.Length) 'Set the first row as table header Dim FRow As TableRow = table.Rows(0) FRow.IsHeader = true 'Set the height and color of the first row FRow.Height = 23 FRow.RowFormat.BackColor = Color.LightSeaGreen Dim i As Integer = 0 Do While (i < Header.Length) 'Set alignment for cells Dim p As Paragraph = FRow.Cells(i).AddParagraph FRow.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle p.Format.HorizontalAlignment = HorizontalAlignment.Center 'Set data format Dim TR As TextRange = p.AppendText(Header(i)) TR.CharacterFormat.FontName = "Calibri" TR.CharacterFormat.FontSize = 12 TR.CharacterFormat.Bold = true i = (i + 1) Loop 'Add data to the rest of rows and set cell format Dim r As Integer = 0 Do While (r < data.Length) Dim DataRow As TableRow = table.Rows((r + 1)) DataRow.Height = 20 Dim c As Integer = 0 Do While (c < data(r).Length) DataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle Dim p2 As Paragraph = DataRow.Cells(c).AddParagraph Dim TR2 As TextRange = p2.AppendText(data(r)(c)) p2.Format.HorizontalAlignment = HorizontalAlignment.Center 'Set data format TR2.CharacterFormat.FontName = "Calibri" TR2.CharacterFormat.FontSize = 11 c = (c + 1) Loop r = (r + 1) Loop 'Save the document doc.SaveToFile("WordTable.docx", FileFormat.Docx2013) End Sub End Class End Namespace
以上便在C#/VB.NET:在 Word 中創(chuàng)建表格,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn