如何向 DOCX C# 添加表格 | IronWord

如何使用IronWord透過 C# 為 DOCX 新增表格

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronWord讓開發人員能夠透過建立具有指定行和列的 Table 對象,設定邊框和顏色樣式,並在儲存為 DOCX 檔案之前填入儲存格內容,從而以程式設計方式將表格新增至 C# Word 文件中。

快速入門:一次呼叫即可建立並儲存表格

本範例示範如何在IronWord中建立表格。 建立表格並設定尺寸,套用樣式,新增內容,插入文件並儲存。 幾分鐘內即可產生具有樣式表格的 DOCX 檔案。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronWord

    PM > Install-Package IronWord
  2. 複製並運行這段程式碼。

    var table = new IronWord.Models.Table(3,4);
    var doc = new IronWord.WordDocument();
    doc.AddTable(table);
    doc.SaveAs("QuickTable.docx");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronWord

    arrow pointer

如何在 Word 文件中新增表格?

表格是 Word 文件的基本組成部分。 首先,透過提供行數和列數來實例化 Table 類別。 配置表格的樣式,包括背景顏色、陰影、邊框、斑馬紋和寬度。 其次,使用直覺的索引存取每個單元格。 在每個單元格中添加文字、圖像、形狀、段落,甚至表格。 最後,將表格新增至 Word 文件。

IronWord中的表格為在 Word 文件中組織結構化資料提供了靈活的基礎。 無論是建立發票、報告或資料摘要,Table 類別都提供了對內容和呈現方式的全面控制。 基於零的索引系統簡化了程序化單元格迭代,而豐富的樣式選項確保了專業的外觀。

所有行和列索引位置均採用從零開始的索引。

: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");
$vbLabelText   $csharpLabel
Word文件中顯示了一個空白的4x3表格,表格包含

AddChild 類別的方法接受一個 ContentElement 對象,該對象包含段落、圖像、形狀和表格。 這樣就可以在複雜的使用場景下使用巢狀表了。

在處理表格儲存格時, IronWord提供了多種內容管理方法。 使用建構子實例化一個 TableCell 並加入初始內容,或使用 AddChild 方法逐步加入內容。 這種靈活性允許建立組合不同內容類型的複雜單元格結構。例如,單一儲存格可以包含標題段落、圖像和用於詳細說明的嵌套表格。

以下範例展示了先進的細胞群體技術:

// Example: Creating cells with mixed content
TableCell complexCell = new TableCell();

// Add a styled paragraph
Paragraph header = new Paragraph();
header.Add(new TextContent("Product Details").Bold().FontSize = 14);
complexCell.AddChild(header);

// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001").Bold());
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();
header.Add(new TextContent("Product Details").Bold().FontSize = 14);
complexCell.AddChild(header);

// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001").Bold());
complexCell.AddChild(new TextContent("\nPrice: $49.99"));

// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord
$vbLabelText   $csharpLabel

我可以對表格套用哪些樣式選項?

IronWord為表格提供了強大的樣式功能,可以建立美觀專業的文件。 除了基本的邊框和顏色之外,還可以透過斑馬紋控制單元格內邊距、對齊方式和應用條件格式。 此樣式系統將強大的功能與直覺的設計相結合,使用熟悉的屬性名稱和清晰的值枚舉。

有哪些邊框樣式可供選擇?

使用 BorderValues 枚舉探索所有可用的邊框值選項:

文字邊框樣式選單顯示單線、雙線、三線、點線、虛線、波浪線以及各種粗細組合等選項。

BorderValues 枚舉提供了全面的表格美觀選項。 從簡單的單線到複雜的圖案,如波浪和點狀圖案,每種風格都有其特定的設計用途。 商務文件可採用專業的雙邊框或三邊框,而創意文件則可使用波浪線或虛線圖案。 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.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.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");
$vbLabelText   $csharpLabel

表格寬度和對齊方式屬性提供了額外的佈局控制。 設定表格的特定寬度或百分比,在文件中對齊表格,並控製表格與周圍內容的互動。 單元格層級的樣式選項包括單獨的背景顏色、文字對齊方式和內邊距調整,從而可以對錶格外觀的每個方面進行精細控制。

這些樣式選項可以建立符合任何文件設計要求的表格,從簡單的資料網格到具有多個視覺層次結構的複雜財務報表。

常見問題解答

如何在 Word 文件中建立具有特定尺寸的表格?

使用 IronWord,您可以透過實體化 Table 類別並指定行數與列數來建立表格。例如,使用「var table = new IronWord.Models.Table(3,4);」來建立一個 3 行 4 欄的表格。然後將其加入 WordDocument 物件,並儲存為 DOCX 檔案。

我可以用程式設定表格的邊框和顏色樣式嗎?

是的,IronWord 允許您設定全面的表格樣式,包括背景顏色、陰影、邊框、斑馬條和寬度。您可以在將表格物件新增至 Word 文件之前,先將這些樣式套用至表格物件。

如何存取表格中的特定儲存格並將其填入?

IronWord 使用基於零的索引來存取表格儲存格。您可以使用直觀的 [row, column] 符號存取儲存格,然後以各種內容類型填充儲存格,包括文字、圖片、圖形、段落,甚至嵌套表格。

我可以在表格單元格中加入哪些類型的內容?

使用 IronWord 的 TableCell 類別,您可以透過 AddChild 方法新增多種內容類型,該方法接受 ContentElement 物件。這包括段落、圖片、圖形,甚至是用來建立巢狀表格結構的表格。

是否可以在表格單元格中建立嵌套表格?

是的,IronWord 支援嵌套表格。由於 AddChild 方法接受包括表格在內的 ContentElement 物件,因此您可以在表格單元格內加入表格,以處理複雜的資料組織需求。

有什麼方法可以快速生成帶表格的 DOCX 檔案?

使用 IronWord 的最快方法是建立具有尺寸的 Table 物件,實體化 WordDocument,使用 AddTable() 新增表格,並使用 SaveAs() 儲存。整個過程只需 4 行代碼即可完成。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 36,374 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronWord
執行範例 觀看您的資料變成 Word doc。