IronWord入門指南

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

IronWord:適用於 .NET 的 Word 文件庫

IronWord是由Iron Software開發的Word文件庫。 IronWord 在為 .NET 應用程式中處理 Word 文件提供強大的功能方面表現出色。

  • 載入、編輯和儲存 Word 和 Docx 文件。
  • PageSetup :配置紙張尺寸、頁面方向、頁邊距和背景顏色。
  • TextRun :處理文字內容、樣式、拆分、追加文字和新增圖像。
  • TextStyle :管理字型系列、大小、顏色、粗體、斜體、刪除線、底線、上標和下標。
  • Paragraph :新增文字行、圖像、形狀、設定樣式、對齊方式、項目符號和編號清單。
  • Table :操作表格結構,包括新增行、取得和設定儲存格值、刪除行、合併儲存格等。
  • Image :從檔案或流載入影像,設定換行文字、位置偏移、寬度、高度和其他屬性。
  • Shape :設定文字環繞、位置偏移、寬度、高度、形狀類型和旋轉。

安裝

IronWord圖書館

安裝 IronWord 快速又簡單。 您可以使用 NuGet 透過以下命令安裝該軟體包:

Install-Package IronWord

或者,直接從IronWord NuGet 官方網站下載。

安裝完成後,您可以透過在 C# 程式碼檔案的頂部新增using IronWord;來開始使用。

應用許可證密鑰

接下來,透過將授權金鑰指派給License類別的LicenseKey屬性,將有效的授權金鑰或試用金鑰套用至 IronWord。 在導入語句之後、使用任何 IronWord 方法之前,先加入以下程式碼:

using IronWord;

// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
using IronWord;

// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
Imports IronWord

' Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
$vbLabelText   $csharpLabel

程式碼範例

讓我們來看一些程式碼範例和可用功能。

請注意IronWord 產生的 DOCX 檔案在特定版本的 Microsoft Word 中開啟時,可能處於相容模式,導致某些樣式無法使用。 若要將 Word 文件從相容模式轉換出去

  1. 選擇"檔案">"資訊",然後點選"轉換"。
  2. 系統會提示您,您的文件將會升級為最新的文件格式。 點選"確定"。

建立 Word 和 Docx 文檔

使用WordDocument類別的建構函式之一實例化該類,即可建立 Word 文件。 之後,使用SaveAs方法匯出 Word 文件。 範例:

using IronWord;

class Program
{
    static void Main()
    {
        // Create a new Word document
        var document = new WordDocument();

        // Save the document as a .docx file
        document.SaveAs("example.docx");
    }
}
using IronWord;

class Program
{
    static void Main()
    {
        // Create a new Word document
        var document = new WordDocument();

        // Save the document as a .docx file
        document.SaveAs("example.docx");
    }
}
Imports IronWord

Friend Class Program
	Shared Sub Main()
		' Create a new Word document
		Dim document = New WordDocument()

		' Save the document as a .docx file
		document.SaveAs("example.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

新增圖片

圖片不能單獨添加; 相反,應將其添加到文件結構中,例如ParagraphTableCellSection 。 使用AddImage方法新增映像。 範例:

using IronWord;
using System.Drawing;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();

        // Add an image to a paragraph
        var paragraph = section.Paragraphs.Add();
        paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));

        document.SaveAs("example_with_image.docx");
    }
}
using IronWord;
using System.Drawing;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();

        // Add an image to a paragraph
        var paragraph = section.Paragraphs.Add();
        paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));

        document.SaveAs("example_with_image.docx");
    }
}
Imports IronWord
Imports System.Drawing

Friend Class Program
	Shared Sub Main()
		Dim document = New WordDocument()
		Dim section = document.Sections.Add()

		' Add an image to a paragraph
		Dim paragraph = section.Paragraphs.Add()
		paragraph.AddImage("path/to/image.jpg", New Rectangle(0, 0, 100, 100))

		document.SaveAs("example_with_image.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

新增表格

新增表格需要建立表格、行、列和表格儲存格。 這提供了很大的配置可能性,因為每個單元格都可以有不同的樣式。 範例:

using IronWord;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();
        var table = section.Tables.Add(3, 3); // 3x3 table

        // Iterate over cells and set their content
        for (int i = 0; i < table.Rows.Count; i++)
        {
            for (int j = 0; j < table.Rows[i].Cells.Count; j++)
            {
                table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
            }
        }

        document.SaveAs("example_with_table.docx");
    }
}
using IronWord;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();
        var table = section.Tables.Add(3, 3); // 3x3 table

        // Iterate over cells and set their content
        for (int i = 0; i < table.Rows.Count; i++)
        {
            for (int j = 0; j < table.Rows[i].Cells.Count; j++)
            {
                table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
            }
        }

        document.SaveAs("example_with_table.docx");
    }
}
Imports IronWord

Friend Class Program
	Shared Sub Main()
		Dim document = New WordDocument()
		Dim section = document.Sections.Add()
		Dim table = section.Tables.Add(3, 3) ' 3x3 table

		' Iterate over cells and set their content
		For i As Integer = 0 To table.Rows.Count - 1
			Dim j As Integer = 0
			Do While j < table.Rows(i).Cells.Count
				table.Rows(i).Cells(j).Paragraphs.Add().AppendText($"Cell {i+1},{j+1}")
				j += 1
			Loop
		Next i

		document.SaveAs("example_with_table.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

提供許可和支持

IronWord是一個付費庫存; 不過,您可以在這裡獲得免費試用許可證。

有關 Iron Software 的更多信息,請訪問我們的網站:https://ironsoftware.com/ 。 如需更多協助或有任何疑問,請聯絡我們的團隊

Iron Software 提供的支持

如需一般支援和技術諮詢,請發送電子郵件至:support@ironsoftware.com

Curtis Chau
技術作家

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

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

準備好開始了嗎?
Nuget 下載 27,129 | Version: 2025.11 剛發表