IronWord 操作指南 新增表格 How to Add Table to DOCX Curtis Chau 更新日期:6月 9, 2025 Download IronWord NuGet 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English A table is a grid of cells arranged in rows and columns. It's used to organize and present information in a structured format. Each intersection of a row and column is a cell, which can contain text, numbers, or other types of data. Tables are commonly used to arrange data neatly, create schedules, or format information in a visually organized manner. Quickstart: Create and Save a Table with One Call This example shows how easy it is to spin up a table in IronWord—just construct it with size, set your styles, add content, drop it into a document, and save. You’ll have a DOCX with a styled table in minutes. Get started making PDFs with NuGet now: Install IronWord with NuGet Package Manager PM > Install-Package IronWord Copy and run this code snippet. var table = new IronWord.Models.Table(3,4); var doc = new IronWord.WordDocument(); doc.AddTable(table); doc.SaveAs("QuickTable.docx"); Deploy to test on your live environment Start using IronWord in your project today with a free trial Free 30 day Trial Minimal Workflow (5 steps) Download a C# library for adding tables to DOCX Populate cells with content and assemble cells into rows Create a table by adding rows to it Initialize a new Word document with the table and append the table Export the final Word document Add Table Example A table is a significant component of a Word document. Firstly, instantiate the Table class by providing the number of rows and columns. From there, the table's styling, such as background color, shading, border, zebra striping, and width, can be customized. Secondly, each cell of the table can be accessed in a very intuitive way by specifying the row and column of the table in [row, column] format. In each cell, text, images, shapes, paragraphs, or even entire tables can be added. Finally, this table can be added to the Word document. 提示All row and column index positions follow zero-based indexing. :path=/static-assets/word/content-code-examples/how-to/add-table-add-table.cs using IronWord; using IronWord.Models; using IronWord.Models.Enums; WordDocument doc = new WordDocument(); // Create table Table table = new Table(5, 3); // Configure border style BorderStyle borderStyle = new BorderStyle(); borderStyle.BorderColor = Color.Black; borderStyle.BorderValue = BorderValues.Thick; borderStyle.BorderSize = 5; // Configure table border TableBorders tableBorders = new TableBorders() { TopBorder = borderStyle, RightBorder = borderStyle, BottomBorder = borderStyle, LeftBorder = borderStyle, }; // Apply styling table.Zebra = new ZebraColor("FFFFFF", "dddddd"); table.Borders = tableBorders; // Populate table table[0, 0] = new TableCell(new TextContent("Number")); table[0, 1] = new TableCell(new TextContent("First Name")); table[0, 2] = new TableCell(new TextContent("Last Name")); for (int i = 1; i < table.Rows.Count; i++) { table[i, 0].AddChild(new TextContent($"{i}")); table[i, 1].AddChild(new TextContent($"---")); table[i, 2].AddChild(new TextContent($"---")); } // Add table doc.AddTable(table); doc.Save("document.docx"); Imports IronWord Imports IronWord.Models Imports IronWord.Models.Enums Private doc As New WordDocument() ' Create table Private table As New Table(5, 3) ' Configure border style Private borderStyle As New BorderStyle() borderStyle.BorderColor = Color.Black borderStyle.BorderValue = BorderValues.Thick borderStyle.BorderSize = 5 ' Configure table border Dim tableBorders As New TableBorders() With { .TopBorder = borderStyle, .RightBorder = borderStyle, .BottomBorder = borderStyle, .LeftBorder = borderStyle } ' Apply styling table.Zebra = New ZebraColor("FFFFFF", "dddddd") table.Borders = tableBorders ' Populate table table(0, 0) = New TableCell(New TextContent("Number")) table(0, 1) = New TableCell(New TextContent("First Name")) table(0, 2) = New TableCell(New TextContent("Last Name")) For i As Integer = 1 To table.Rows.Count - 1 table(i, 0).AddChild(New TextContent($"{i}")) table(i, 1).AddChild(New TextContent($"---")) table(i, 2).AddChild(New TextContent($"---")) Next i ' Add table doc.AddTable(table) doc.Save("document.docx") $vbLabelText $csharpLabel The AddChild method of the TableCell class accepts a ContentElement object, which includes everything from paragraphs, images, and shapes to the table itself. In this case, you can have nested tables, providing a very useful illustration for certain use cases. Available Stylings Borders Explore all the available options for border values that can be set using the BorderValues enum: 常見問題解答 如何使用C#向Word文档添加表格? 您可以通过下载C#库、用内容填充单元格、将它们组装成行来使用IronWord向Word文档添加表格,并通过添加这些行来创建表格。一旦表格结构设置好后,在新的Word文档中初始化并导出该文档。 IronWord中的表格有哪些样式选项可用? IronWord允许您自定义表格的多种样式选项,例如背景颜色、阴影、边框、斑马条纹和宽度。 如何在C#中向表格的特定单元格添加内容? 在IronWord中,您可以使用TableCell类的AddChild方法向特定单元格添加内容,如文本、图像、形状,甚至整个表格。 我能用IronWord以编程方式在其他表格中嵌套表格吗? 是的,IronWord允许您在其他表格中嵌套表格,这对于复杂的文档布局特别有用。 在IronWord中访问表格单元格的索引格式是什么? IronWord使用从零开始的索引格式,指定为[row, column]格式来访问表格单元格。 如何使用C#为Word文档中的表格设置边框样式? 您可以使用IronWord的BorderValues枚举来设置表格边框样式,该枚举提供了各种自定义表格边框的选项。 在使用IronWord添加表格后,使用什么方法保存Word文档? 在使用IronWord向Word文档添加表格后,您可以使用Save方法保存文档,并指定所需的文件名,例如document.Save('ExampleTable.docx')。 在IronWord中可以向表格单元格添加哪些类型的内容? 在IronWord中,您可以向表格单元格添加各种类型的内容,包括文本、图像、形状、段落,甚至嵌套表格。 如何在IronWord中初始化一个表格? 要在IronWord中初始化一个表格,需要通过提供行和列的数量来实例化Table类。例如,var table = new Table(3, 3)创建一个3x3的表格。 使用C#导出带有表格的Word文档的过程是什么? 要使用IronWord导出带有表格的Word文档,首先创建和样式化表格,在Word文档中初始化它,然后使用Save方法将文档导出为.docx文件。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 25,807 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:25,807 查看許可證