使用 IronWord 在 C# 中將表格新增至 DOCX 的方法
開發者可使用 IronWord 在 C# 程式中將表格新增至 Word 文件。先建立具有指定列數與欄數的 Table 物件,接著設定邊框與色彩、在儲存格中加入內容,最後儲存為 DOCX 檔案。
本範例示範如何使用 IronWord 建立表格。指定表格的列數與欄數後,套用樣式並新增內容,再插入文件並儲存。您可以在幾分鐘內產生包含已套用樣式之表格的 DOCX 檔案。
-
1使用 IronWord 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var table = new IronWord.Models.Table(3,4); var doc = new IronWord.WordDocument(); doc.AddTable(table); doc.SaveAs("QuickTable.docx");Dim table As New IronWord.Models.Table(3, 4) Dim doc As New IronWord.WordDocument() doc.AddTable(table) doc.SaveAs("QuickTable.docx")Dim table As New IronWord.Models.Table(3, 4) Dim doc As New IronWord.WordDocument() doc.AddTable(table) doc.SaveAs("QuickTable.docx") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronWord,並使用 免費試用
基本流程(5 個步驟)
- 下載可將表格新增至 DOCX 的 C# 程式庫
- 在儲存格中加入內容,再將儲存格組成列
- 新增各列以建立表格
- 使用表格初始化新的 Word 文件,並附加表格
- 匯出完成的 Word 文件
如何將表格新增至 Word 文件?
表格是 Word 文件的基本元素。先指定列數與欄數來建立 Table 執行個體,再設定背景色、網底、邊框、交錯列底色及寬度等樣式。接著使用 [row, column] 索引存取各個儲存格,可在其中新增文字、圖片、圖案、段落,甚至其他表格。最後,將表格新增至 Word 文件。
IronWord 的表格可靈活組織 Word 文件中的結構化資料。無論是建立發票、報表或資料摘要,Table 類別都能讓您控制內容與呈現方式。從 0 開始的索引可簡化以程式逐一存取儲存格的操作,多種樣式選項也讓文件呈現專業外觀。
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.Style.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");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.Style.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
Dim doc As New WordDocument()
' Create table
Dim table As New Table(5, 3)
' Configure border style
Dim 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.Style.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
' Add table
doc.AddTable(table)
doc.Save("document.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Dim doc As New WordDocument()
' Create table
Dim table As New Table(5, 3)
' Configure border style
Dim 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.Style.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
' Add table
doc.AddTable(table)
doc.Save("document.docx")
TableCell 類別的 AddChild 方法接受實作 IWordDocumentObject 的內容物件,例如段落、圖片、圖案及表格。因此,您可以在較複雜的情境中使用巢狀表格。
IronWord 提供多種管理儲存格內容的方式。建立 TableCell 時,可以透過建構函式傳入初始內容,也可以用 AddChild 方法逐步新增。這樣就能在同一個儲存格中組合不同類型的內容,例如先放標題段落,再加入圖片和列出詳細規格的巢狀表格。
以下範例示範如何在儲存格中加入較複雜的內容:
// Example: Creating cells with mixed content
TableCell complexCell = new TableCell();
// Add a styled paragraph
Paragraph header = new Paragraph();
TextContent headerText = new TextContent("Product Details") { IsBold = true, FontSize = 14 };
header.AddText(headerText);
complexCell.AddChild(header);
// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001") { IsBold = true });
complexCell.AddChild(new TextContent("\nPrice: $49.99"));
// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord// Example: Creating cells with mixed content
TableCell complexCell = new TableCell();
// Add a styled paragraph
Paragraph header = new Paragraph();
TextContent headerText = new TextContent("Product Details") { IsBold = true, FontSize = 14 };
header.AddText(headerText);
complexCell.AddChild(header);
// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001") { IsBold = true });
complexCell.AddChild(new TextContent("\nPrice: $49.99"));
// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord' Example: Creating cells with mixed content
Dim complexCell As New TableCell()
' Add a styled paragraph
Dim header As New Paragraph()
Dim headerText As New TextContent("Product Details") With {.IsBold = True, .FontSize = 14}
header.AddText(headerText)
complexCell.AddChild(header)
' Add multiple text elements
complexCell.AddChild(New TextContent("SKU: "))
complexCell.AddChild(New TextContent("PROD-001") With {.IsBold = True})
complexCell.AddChild(New TextContent(vbCrLf & "Price: $49.99"))
' Cells can also contain lists, images, and more
' This demonstrates the versatility of table cells in IronWord' Example: Creating cells with mixed content
Dim complexCell As New TableCell()
' Add a styled paragraph
Dim header As New Paragraph()
Dim headerText As New TextContent("Product Details") With {.IsBold = True, .FontSize = 14}
header.AddText(headerText)
complexCell.AddChild(header)
' Add multiple text elements
complexCell.AddChild(New TextContent("SKU: "))
complexCell.AddChild(New TextContent("PROD-001") With {.IsBold = True})
complexCell.AddChild(New TextContent(vbCrLf & "Price: $49.99"))
' Cells can also contain lists, images, and more
' This demonstrates the versatility of table cells in IronWord表格可套用哪些樣式?
IronWord 提供多種表格樣式設定,可讓文件看起來更專業。除了基本的邊框和色彩,您還可以調整儲存格的內距與對齊方式,並透過交錯列底色套用條件式格式。這些設定使用熟悉的屬性名稱和清楚的列舉值,方便操作。
有哪些邊框樣式可用?
使用 BorderValues 列舉查看所有可用的邊框樣式:

BorderValues 列舉包含各種邊框樣式,從簡單的單線到波浪線和點線等較複雜的圖案。商務文件可用 Double 或 Triple 邊框呈現專業外觀,設計較活潑的文件則可使用 Wave 或 DashDot。搭配 BorderSize 和 BorderValue,可精確控制線條粗細,單位為八分之一點。
以下範例示範幾種邊框設定:
// Example: Applying different borders to table sections
Table styledTable = new Table(4, 4);
// Create distinct border styles for header and body
BorderStyle headerBorder = new BorderStyle
{
BorderColor = Color.Navy,
BorderValue = BorderValues.Double,
BorderSize = 8
};
BorderStyle bodyBorder = new BorderStyle
{
BorderColor = Color.Gray,
BorderValue = BorderValues.Dotted,
BorderSize = 3
};
// Apply different borders to different parts of the table
// This creates visual hierarchy and improves readability
styledTable.Style.Borders = new TableBorders
{
TopBorder = headerBorder,
BottomBorder = headerBorder,
LeftBorder = bodyBorder,
RightBorder = bodyBorder,
InsideHorizontalBorder = bodyBorder,
InsideVerticalBorder = bodyBorder
};
// Zebra striping for better row distinction
styledTable.Zebra = new ZebraColor("F5F5F5", "FFFFFF");// Example: Applying different borders to table sections
Table styledTable = new Table(4, 4);
// Create distinct border styles for header and body
BorderStyle headerBorder = new BorderStyle
{
BorderColor = Color.Navy,
BorderValue = BorderValues.Double,
BorderSize = 8
};
BorderStyle bodyBorder = new BorderStyle
{
BorderColor = Color.Gray,
BorderValue = BorderValues.Dotted,
BorderSize = 3
};
// Apply different borders to different parts of the table
// This creates visual hierarchy and improves readability
styledTable.Style.Borders = new TableBorders
{
TopBorder = headerBorder,
BottomBorder = headerBorder,
LeftBorder = bodyBorder,
RightBorder = bodyBorder,
InsideHorizontalBorder = bodyBorder,
InsideVerticalBorder = bodyBorder
};
// Zebra striping for better row distinction
styledTable.Zebra = new ZebraColor("F5F5F5", "FFFFFF");' Example: Applying different borders to table sections
Dim styledTable As New Table(4, 4)
' Create distinct border styles for header and body
Dim headerBorder As New BorderStyle With {
.BorderColor = Color.Navy,
.BorderValue = BorderValues.Double,
.BorderSize = 8
}
Dim bodyBorder As New BorderStyle With {
.BorderColor = Color.Gray,
.BorderValue = BorderValues.Dotted,
.BorderSize = 3
}
' Apply different borders to different parts of the table
' This creates visual hierarchy and improves readability
styledTable.Style.Borders = New TableBorders With {
.TopBorder = headerBorder,
.BottomBorder = headerBorder,
.LeftBorder = bodyBorder,
.RightBorder = bodyBorder,
.InsideHorizontalBorder = bodyBorder,
.InsideVerticalBorder = bodyBorder
}
' Zebra striping for better row distinction
styledTable.Zebra = New ZebraColor("F5F5F5", "FFFFFF")' Example: Applying different borders to table sections
Dim styledTable As New Table(4, 4)
' Create distinct border styles for header and body
Dim headerBorder As New BorderStyle With {
.BorderColor = Color.Navy,
.BorderValue = BorderValues.Double,
.BorderSize = 8
}
Dim bodyBorder As New BorderStyle With {
.BorderColor = Color.Gray,
.BorderValue = BorderValues.Dotted,
.BorderSize = 3
}
' Apply different borders to different parts of the table
' This creates visual hierarchy and improves readability
styledTable.Style.Borders = New TableBorders With {
.TopBorder = headerBorder,
.BottomBorder = headerBorder,
.LeftBorder = bodyBorder,
.RightBorder = bodyBorder,
.InsideHorizontalBorder = bodyBorder,
.InsideVerticalBorder = bodyBorder
}
' Zebra striping for better row distinction
styledTable.Zebra = New ZebraColor("F5F5F5", "FFFFFF")表格的寬度和對齊屬性可進一步控制版面。您可以指定固定寬度或百分比、設定表格在文件中的對齊方式,並控制表格與周圍內容的配置。也可以針對個別儲存格設定背景色、文字對齊與內距,以便細部調整表格的外觀。
這些樣式選項可因應各種文件的設計需求,從簡單的資料表格到具有多層視覺層次的財務報表。
常見問題
如何使用C#在DOCX文件中創建表格?
您可以使用 IronWord 通過實例化 Table 類並指定所需的列數和欄數來在 DOCX 文件中創建表格。在設置好樣式和內容後,您可以將表格添加到 WordDocument 並將其儲存為 DOCX 文件。
在IronWord中,表格有哪些樣式選項可用?
IronWord 提供廣泛的表格樣式選項,包括邊框、顏色、單元格內距和對齊方式。您可以應用像斑馬條紋這樣的條件格式,並使用 BorderValues 列舉的各種邊框樣式來增強表格美感。
我可以使用IronWord向各個表格單元格添加複雜內容嗎?
是的,IronWord 允許您使用 AddChild 方法向個別表格單元格添加複雜內容。單元格可以包含嵌套表格、段落、圖像和各種文字元素,實現多樣且詳盡的內容管理。
如何在IronWord創建的表格中使用斑馬條紋?
要在 IronWord 的表格中應用斑馬條紋,配置表格樣式的 ZebraColor 屬性,指定交替的列顏色以提高可讀性和視覺吸引力。
使用IronWord將表格添加到Word文檔的步驟有哪些?
要使用 IronWord 向 Word 文件添加表格,您需要下載 IronWord 程式庫,創建並設置表格樣式,然後將其插入到 WordDocument,最後保存文檔。
如何在IronWord中控制表格的寬度和對齊?
在IronWord中,您可以通過設置表格的相關屬性來控制其寬度和對齊。這使您能夠指定確切的尺寸或百分比,並根據需要在文檔中對齊表格。
在IronWord中有哪些方法可以用數據填充表格?
在 IronWord 中,您可以通過零基索引訪問單元格,並使用 AddChild 等方法程式化地插入 TextContent、圖像或其他元素填充表格。
IronWord如何支持表格的邊框樣式?
IronWord 支援通過 BorderStyle 類自定義邊框顏色、值和大小來進行邊框樣式。您可以在表格的標題和正文部分創建獨特的視覺層次結構。
我可以在IronWord中將表格嵌套在單元格內嗎?
是的,IronWord 支援使用 AddChild 方法在單元格內嵌套表格。此功能有助於為複雜文檔構建複雜的表格結構。
使用IronWord在Word文件中創建表格有哪些好處?
使用IronWord創建表格提供了一種靈活且程式化的文檔結構方法,允許全面的內容管理、詳細的樣式設置和高效的自動化來生成專業的DOCX文件。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。