跳過到頁腳內容
產品比較

IronWord 與 Microsoft.Office.Interop.Word 的比較

1.簡介

許多開發人員在使用 .NET 應用程式時需要處理 Word 文件。 這可用於建立報表、處理文件或產生發票。 實現此功能的兩個常用工具是 Microsoft.Office.Interop.Word 和 IronWord。 它們各自都有其自身的特質、優點和缺點。

本文將比較Microsoft.Office.Interop.WordIronWord 。 我們將探討每種工具的功能、效能以及最適合使用的場景。 這應該可以幫助您決定哪種工具更適合您的特定需求。

2. Microsoft.Office.Interop.Word

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 1 - Microsoft.Office.Interop.Word 網頁

Microsoft.Office.Interop.Word 是一個函式庫,它使 .NET 應用程式能夠直接與 Microsoft Word 文件進行互動。 對於需要自動化 Word 文件處理流程或將 Word 文件操作功能整合到 .NET 應用程式中的開發人員來說,這是一個關鍵元件。 該程式庫對於在 .NET 環境中執行文件產生、修改和格式化等任務特別有用,可以充分利用 Microsoft Word 的全部功能。

2.1 主要功能

文件建立和編輯:

Microsoft.Office.Interop.Word 為建立和編輯 Word 文件提供了廣泛的支援。 它允許開發人員以程式設計方式新增、修改和刪除文本,以及管理文件屬性,例如標題和主題。 此功能對於根據使用者輸入或外部資料來源產生動態文件至關重要。

格式和样式:

該庫提供了全面的文字、段落和整個文件格式設定選項。 開發人員可以套用各種樣式、字型、顏色和段落對齊方式來建立專業格式的文件。 這包括定義和套用自訂樣式以覆蓋預設樣式的功能,確保產生的文件符合特定的品牌或設計要求。

與 Microsoft Word 的兼容性:

作為 Microsoft Office 套件的一個元件,Microsoft.Office.Interop.Word 確保與 Microsoft Word 的所有功能高度相容。 這包括與最新版本的 Word 無縫集成,確保透過庫建立或修改的文件與 Word 的原生格式完全相容。

3. 鐵詞

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 2 - IronWord 網頁

IronWord 是一個 .NET 函式庫,專為文件操作而設計,專門用於 Word 文件處理。 與依賴 Microsoft Office 套件的 Microsoft.Office.Interop.Word 不同,IronWord 提供了一個獨立的解決方案,用於在 .NET 應用程式中建立、編輯和轉換 Word 文件。 對於希望使用無需在伺服器或用戶端電腦上安裝 Microsoft Word 的程式庫的開發人員來說,這是一個很有吸引力的選擇。

3.1 主要功能

獨立文件處理:

IronWord 允許在 .NET 應用程式中直接操作 Word 文檔,而無需安裝 Microsoft Office Word。 這種獨立性簡化了部署,降低了開發環境的複雜性,尤其是在伺服器或雲端環境中。

文檔轉換:

IronWord 的一項重要功能是支援將 Word 文件轉換為其他格式,例如 PDF、HTML 和純文字。 對於需要以多種格式呈現文件的應用程序,或者需要以 HTML 格式顯示文件內容的 Web 應用程式來說,此功能至關重要。

全面的格式選項:

IronWord 提供多種格式設定功能,讓開發人員套用文字樣式、新增段落對齊方式以及插入表單和圖像等元素。 這些選項可確保 IronWord 產生或修改的文件符合所需的視覺和結構標準。

輕鬆整合到 .NET 專案中:

IronWord 的設計充分考慮了 .NET 應用程式的需求,可以透過 NuGet 輕鬆整合到任何 .NET 專案中。 它的 API 設計得對 .NET 開發人員來說非常直觀,使他們能夠輕鬆地開始處理 Word 文件。

跨平台相容性:

IronWord 支援跨平台開發,因此適用於各種作業系統的應用程式。 對於需要在 Windows 作業系統和 Linux 作業系統上運行,或者在雲端環境中運行的專案來說,這一點尤其有價值。

4. 在 Visual Studio 中建立 .NET 項目

4.1 在 Visual Studio 中開啟

首先啟動 Microsoft Visual Studio。 打開應用程式後,您將看到啟動視窗。 此視窗提供多種入門選項,包括開啟現有專案或建立新專案。 就我們的目的而言,你將專注於啟動一個新專案。

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 3 - 在 Visual Studio 中建立新項目

4.2 選擇項目類型

點選建立新專案選項後,Visual Studio 會顯示範本清單。 若要繼續,請選擇控制台應用程式項目。

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 4 - 建立控制台應用程式項目

4.3 項目配置

選擇所需的項目範本後,點選"下一步"即可進入專案配置畫面。 在這裡,您需要提供有關您專案的基本資訊。 這包括指定專案名稱和選擇專案檔案的儲存位置,例如程式檔案資料夾,該資料夾保留供應用程式內部使用。

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 5 - 設定專案名稱與檔案路徑

下一個對話框用於選擇目標框架。 你選擇的 .NET Framework 決定了你的應用程式可用的函式庫和 API,進而影響其相容性和功能。 請務必選擇支援您計劃在應用程式中實現的功能的框架版本,並利用 Visual Studio 工具獲得最佳化的開發體驗。

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 6 - 選擇適當的 .NET 框架

4.4 建立項目

填寫完必要資訊並確保所有設定都已正確調整以滿足專案要求後,最後一步就是建立專案。 點擊"創建"按鈕即可實現。 然後,Visual Studio 會根據選取的範本和配置初始化一個新專案。 這個過程可能需要幾分鐘時間,之後您的新 .NET 專案即可開啟並進行開發。

5. 安裝 IronWord 庫

本節將深入探討如何使用 NuGet 套件管理器、Visual Studio 命令列以及直接從 NuGet 網頁下載的方式將 IronWord 庫整合到您的專案中。

5.1 使用 NuGet 套件管理員。

NuGet 套件管理器是 Visual Studio 中一個使用者友善的介面,可以無縫安裝程式庫和工具。 若要使用此方法安裝 IronWord 庫,請依照下列步驟操作:

1.開啟 NuGet 套件管理員:開啟專案後,在解決方案資源管理器中以滑鼠右鍵按一下專案名稱,然後從上下文功能表中選擇"管理 NuGet 套件..."。 此操作將開啟 NuGet 套件管理器視窗。 2.搜尋 IronWord :在 NuGet 套件管理器視窗中,導覽至"瀏覽"標籤。 在搜尋框中輸入"IronWord"並按下回車鍵,即可找到 IronWord 庫。

[IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 7 - 使用 NuGet 套件管理器安裝 IronWord](/static-assets/word/blog/microsoft-office-interop-word-csharp-example-alternatives/microsoft-office-interop-word-csharp-example-alternatives-7.webp)

3.安裝 IronWord :從搜尋結果中選擇 IronWord 軟體包。 您將看到詳細描述和版本資訊。 選擇符合您專案需求的版本,然後按一下"安裝"按鈕。 Visual Studio 隨後會處理程式庫的下載和安裝,並相應地更新專案參考。

5.2 使用 Visual Studio 命令列。

對於喜歡使用命令列工具的用戶,Visual Studio 提供了套件管理器控制台,這是一個用於管理 NuGet 套件的 PowerShell 介面。

1.存取套件管理員控制台:從 Visual Studio 的主選單導覽至"工具">"NuGet 套件管理員">"套件管理器控制台"。 2.安裝 IronWord :在控制台中,輸入以下指令並按 Enter 鍵:

```powershell
Install-Package IronWord
```

此命令會取得 IronWord 庫的最新版本並將其整合到您的專案中。 您可以透過新增-Version參數並在其後新增版本號來指定特定版本。

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 8 - 透過 VS 命令列安裝 IronWord

5.3 直接從 NuGet 網頁下載。

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 9 - 透過 NuGet 網頁安裝 IronWord

如果您喜歡手動管理專案的程式庫,可以直接從 NuGet 網站下載 IronWord 套件並將其新增至您的專案。

1.下載軟體包:造訪nuget.org並搜尋 IronWord 軟體包。 在軟體包頁面找到"下載"鏈接,獲取.nupkg檔。將此文件儲存到您的電腦。 2.新增至專案:下載完成後,您可以手動將軟體包新增至您的專案。 該過程通常包括提取.nupkg文件,然後在項目中添加對其中包含的組件的參考。 不過,依照上述方法,使用 NuGet 套件管理器或 Visual Studio 命令列通常更直接,出錯的幾率也更小。

6. 安裝 Microsoft.Office.Interop.Word

可以透過多種方式將此程式庫新增至您的專案中,每種方式都適合不同的開發偏好和要求。

6.1 使用 NuGet 套件管理器

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 10 - 透過 NuGet 套件管理器安裝 Microsoft.Office.Interop.Word

Visual Studio 中的 NuGet 套件管理器是為專案中新增 Microsoft.Office.Interop.Word 程式庫的最直接方法。 請遵循以下步驟:

1.開啟 NuGet 套件管理器:在 Visual Studio 中開啟專案後,在解決方案資源管理器中以滑鼠右鍵按一下專案名稱。 從上下文功能表中選擇"管理 NuGet 套件..."以開啟 NuGet 套件管理員視窗。 2.搜尋庫:在 NuGet 套件管理器視窗中,切換到"瀏覽"選項卡,然後在搜尋框中鍵入"Microsoft.Office.Interop.Word"。 按下回車鍵開始搜尋。 3.安裝軟體包:在搜尋結果中找到"Microsoft.Office.Interop.Word"軟體包。 請查看提供的版本資訊和說明,以確保其滿足您的專案需求。 點擊"安裝"按鈕,將庫新增到您的專案中。 Visual Studio 將處理程式包的下載和安裝,並更新專案參考以包含必要的組件。

6.2 使用 Visual Studio 命令列

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 11 - 透過 VS 命令列安裝 Microsoft.Office.Interop.Word

Visual Studio 中的命令列介面"套件管理器控制台"提供了另一種安裝 Microsoft.Office.Interop.Word 程式庫的方法。

1.存取套件管理員控制台:從 Visual Studio 的主選單到"工具">"NuGet 套件管理員">"套件管理器控制台"。 2.執行安裝指令:在控制台中,輸入以下指令並按 Enter 鍵:

```powershell
Install-Package Microsoft.Office.Interop.Word
```

此命令指示 Visual Studio 取得庫的最新版本並將其整合到您的專案中。 如果您需要特定版本的函式庫,可以透過附加-Version參數並後面跟著所需的版本號來指定。

6.3 從 NuGet 網頁直接下載

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 12 - 從 NuGet 網頁安裝 Microsoft.Office.Interop.Word

對於喜歡手動管理專案依賴項的開發人員,可以直接從 NuGet 網站下載 Microsoft.Office.Interop.Word 程式庫。

1.下載軟體包:造訪nuget.org並搜尋"Microsoft.Office.Interop.Word"。 在軟體包頁面上,找到"下載"連結以取得.nupkg檔。將此文件儲存到您電腦上的某個已知位置。 2.手動將軟體包新增至您的專案:下載後,可以將.nupkg檔案手動新增至您的專案。 這通常涉及提取文件,並在專案中添加對提取的程序集的引用。 不過,為了方便使用並最大限度地減少潛在錯誤,建議使用 NuGet 套件管理器或 Visual Studio 命令列。

7. IronWord 和 Microsoft.Office.Interop.Word 進階功能的比較

7.1 IronWord 的進階功能

7.1.1閱讀和編輯 Word 文檔

IronWord 允許讀取和修改 Word 文件。 這包括開啟現有文件、進行更改,並將更改儲存回原始文件或新文件。對於需要根據使用者輸入或其他來源的資料更新文件的應用程式而言,此功能至關重要。

using IronWord;
using IronWord.Models;

// Initialize a new Word document
WordDocument document = new WordDocument("my_new_document.docx");

// Insert new text content into the document
document.AddText("This is a demonstration of modifying content with IronWord.");

// Save the document with a new filename
document.SaveAs("updated_document.docx");
using IronWord;
using IronWord.Models;

// Initialize a new Word document
WordDocument document = new WordDocument("my_new_document.docx");

// Insert new text content into the document
document.AddText("This is a demonstration of modifying content with IronWord.");

// Save the document with a new filename
document.SaveAs("updated_document.docx");
Imports IronWord
Imports IronWord.Models

' Initialize a new Word document
Private document As New WordDocument("my_new_document.docx")

' Insert new text content into the document
document.AddText("This is a demonstration of modifying content with IronWord.")

' Save the document with a new filename
document.SaveAs("updated_document.docx")
$vbLabelText   $csharpLabel

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 13 - 前一程式碼的輸出文檔

7.1.2編輯頁面設置

IronWord 可以更改文件的紙張尺寸,以適應 A4、Letter 或 Legal 等各種標準。 您可以根據文件要求,在縱向和橫向之間調整頁面方向。

7.1.3新增段落

使用 IronWord 以程式設計方式新增段落非常簡單,可以根據應用程式邏輯動態插入文字。

using IronWord;
using IronWord.Models;

// Initialize a new Word document
WordDocument document = new WordDocument();

// Insert text into the document
document.AddText("Exploring IronWord Capabilities");

// Save the document to a new file
document.SaveAs("updated_ironword_document.docx");
using IronWord;
using IronWord.Models;

// Initialize a new Word document
WordDocument document = new WordDocument();

// Insert text into the document
document.AddText("Exploring IronWord Capabilities");

// Save the document to a new file
document.SaveAs("updated_ironword_document.docx");
Imports IronWord
Imports IronWord.Models

' Initialize a new Word document
Private document As New WordDocument()

' Insert text into the document
document.AddText("Exploring IronWord Capabilities")

' Save the document to a new file
document.SaveAs("updated_ironword_document.docx")
$vbLabelText   $csharpLabel

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 14 - 前一程式碼的輸出文檔

7.1.4新增和刪除文字運行

TextRuns 表示具有一組共同屬性的文字段。 IronWord 允許新增和刪除這些內容,使開發人員能夠控製文字分割和屬性。

7.1.5新增圖片

IronWord 支援在文件中新增影像,包括指定影像的位置、環繞方式和大小,從而增強文件的視覺吸引力和有效性。

using IronWord;
using IronWord.Models;

// Initialize a new Word document
WordDocument document = new WordDocument();

// Setup an image
IronWord.Models.Image img = new IronWord.Models.Image("updated_image.jpg");
img.Width = 250; // Width in pixels
img.Height = 250; // Height in pixels

// Create a paragraph to contain the image
Paragraph para = new Paragraph();
// Incorporate an image into the paragraph
para.AddImage(img);

// Add the paragraph to the document
document.AddParagraph(para);

// Save the document with a new name
document.SaveAs("updated_save_document.docx");
using IronWord;
using IronWord.Models;

// Initialize a new Word document
WordDocument document = new WordDocument();

// Setup an image
IronWord.Models.Image img = new IronWord.Models.Image("updated_image.jpg");
img.Width = 250; // Width in pixels
img.Height = 250; // Height in pixels

// Create a paragraph to contain the image
Paragraph para = new Paragraph();
// Incorporate an image into the paragraph
para.AddImage(img);

// Add the paragraph to the document
document.AddParagraph(para);

// Save the document with a new name
document.SaveAs("updated_save_document.docx");
Imports IronWord
Imports IronWord.Models

' Initialize a new Word document
Private document As New WordDocument()

' Setup an image
Private img As New IronWord.Models.Image("updated_image.jpg")
img.Width = 250 ' Width in pixels
img.Height = 250 ' Height in pixels

' Create a paragraph to contain the image
Dim para As New Paragraph()
' Incorporate an image into the paragraph
para.AddImage(img)

' Add the paragraph to the document
document.AddParagraph(para)

' Save the document with a new name
document.SaveAs("updated_save_document.docx")
$vbLabelText   $csharpLabel

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 15 - 包含先前程式碼產生的映像的輸出文檔

7.1.6新增形狀

使用 IronWord 可以為文件中新增形狀,包括設定形狀類型、大小、位置和旋轉角度。 這增加了一層視覺趣味,可以用來突出訊息或添加裝飾元素。

7.1.7設定樣式

IronWord 的樣式選項非常豐富,涵蓋字體系列和大小、顏色以及文字裝飾,例如粗體、斜體、刪除線、底線、上標和下標。 這種程度的控制對於維護品牌標準和確保文件可讀性至關重要。

using IronSoftware.Drawing;
using IronWord;
using IronWord.Models;
using Color = IronSoftware.Drawing.Color;

// Initialize WordDocument with a file
WordDocument document = new WordDocument("updated_document.docx");

// Create a TextRun with modifications
TextRun modifiedTextRun = new TextRun
{
    Text = "Updated text with IronWord",
    Style = new TextStyle
    {
        FontFamily = "Arial",
        FontSize = 48, // Adjusted font size
        TextColor = new IronColor(Color.Blue),
        IsBold = false,
        IsItalic = false,
        IsUnderline = false,
        IsSuperscript = true,
        IsStrikethrough = false,
        IsSubscript = true
    }
};

// Create a paragraph and add the TextRun
Paragraph newParagraph = new Paragraph();
newParagraph.AddTextRun(modifiedTextRun);

// Add the paragraph to the document
document.AddParagraph(newParagraph);

// Save the document with a new name
document.SaveAs("updated_save_document.docx");
using IronSoftware.Drawing;
using IronWord;
using IronWord.Models;
using Color = IronSoftware.Drawing.Color;

// Initialize WordDocument with a file
WordDocument document = new WordDocument("updated_document.docx");

// Create a TextRun with modifications
TextRun modifiedTextRun = new TextRun
{
    Text = "Updated text with IronWord",
    Style = new TextStyle
    {
        FontFamily = "Arial",
        FontSize = 48, // Adjusted font size
        TextColor = new IronColor(Color.Blue),
        IsBold = false,
        IsItalic = false,
        IsUnderline = false,
        IsSuperscript = true,
        IsStrikethrough = false,
        IsSubscript = true
    }
};

// Create a paragraph and add the TextRun
Paragraph newParagraph = new Paragraph();
newParagraph.AddTextRun(modifiedTextRun);

// Add the paragraph to the document
document.AddParagraph(newParagraph);

// Save the document with a new name
document.SaveAs("updated_save_document.docx");
Imports IronSoftware.Drawing
Imports IronWord
Imports IronWord.Models
Imports Color = IronSoftware.Drawing.Color

' Initialize WordDocument with a file
Private document As New WordDocument("updated_document.docx")

' Create a TextRun with modifications
Private modifiedTextRun As New TextRun With {
	.Text = "Updated text with IronWord",
	.Style = New TextStyle With {
		.FontFamily = "Arial",
		.FontSize = 48,
		.TextColor = New IronColor(Color.Blue),
		.IsBold = False,
		.IsItalic = False,
		.IsUnderline = False,
		.IsSuperscript = True,
		.IsStrikethrough = False,
		.IsSubscript = True
	}
}

' Create a paragraph and add the TextRun
Private newParagraph As New Paragraph()
newParagraph.AddTextRun(modifiedTextRun)

' Add the paragraph to the document
document.AddParagraph(newParagraph)

' Save the document with a new name
document.SaveAs("updated_save_document.docx")
$vbLabelText   $csharpLabel

7.1.8設定對齊方式

IronWord 支援設定文件內的文字對齊方式,包括左對齊、居中對齊、右對齊和兩端對齊。 這對於美觀和可讀性都很重要。

7.1.10新增表格

表格是許多文件的重要組成部分,用於組織資料和資訊。 IronWord 允許新增表格並操作其結構,包括新增或刪除行和列。 使用 IronWord 可以輕鬆操作表格單元格的內容,並可根據應用程式的需求動態插入內容。 IronWord 提供了合併和分割表格儲存格的功能,這對於自訂表格佈局和滿足複雜的資料呈現要求非常有用。

using IronWord;
using IronWord.Models;

// Create a table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Updated content"; // Changed the text content

// Add a TextRun to the cell
cell.AddContent(new Paragraph(textRun));

// Configure border style
BorderStyle borderStyle = new BorderStyle
{
    BorderColor = new IronColor(IronSoftware.Drawing.Color.Blue), // Changed border color to blue
    BorderValue = IronWord.Models.Enums.BorderValues.Double, // Changed border style to double
    BorderSize = 3 // Adjusted border size to 3
};

// Configure table border
TableBorders tableBorders = new TableBorders
{
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};

// Assign borders to the cell
cell.Borders = tableBorders;

// Create a row and add the cell
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);

// Create a table and add the row
Table table = new Table();
table.AddRow(row);

// Create a new Word document from the table
WordDocument doc = new WordDocument(table);

// Export the Word document
doc.SaveAs("UpdatedDocument.docx");
using IronWord;
using IronWord.Models;

// Create a table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Updated content"; // Changed the text content

// Add a TextRun to the cell
cell.AddContent(new Paragraph(textRun));

// Configure border style
BorderStyle borderStyle = new BorderStyle
{
    BorderColor = new IronColor(IronSoftware.Drawing.Color.Blue), // Changed border color to blue
    BorderValue = IronWord.Models.Enums.BorderValues.Double, // Changed border style to double
    BorderSize = 3 // Adjusted border size to 3
};

// Configure table border
TableBorders tableBorders = new TableBorders
{
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};

// Assign borders to the cell
cell.Borders = tableBorders;

// Create a row and add the cell
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);

// Create a table and add the row
Table table = new Table();
table.AddRow(row);

// Create a new Word document from the table
WordDocument doc = new WordDocument(table);

// Export the Word document
doc.SaveAs("UpdatedDocument.docx");
Imports IronWord
Imports IronWord.Models

' Create a table cell
Private cell As New TableCell()
Private textRun As New TextRun()
textRun.Text = "Updated content" ' Changed the text content

' Add a TextRun to the cell
cell.AddContent(New Paragraph(textRun))

' Configure border style
Dim borderStyle As New BorderStyle With {
	.BorderColor = New IronColor(IronSoftware.Drawing.Color.Blue),
	.BorderValue = IronWord.Models.Enums.BorderValues.Double,
	.BorderSize = 3
}

' Configure table border
Dim tableBorders As New TableBorders With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}

' Assign borders to the cell
cell.Borders = tableBorders

' Create a row and add the cell
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)

' Create a table and add the row
Dim table As New Table()
table.AddRow(row)

' Create a new Word document from the table
Dim doc As New WordDocument(table)

' Export the Word document
doc.SaveAs("UpdatedDocument.docx")
$vbLabelText   $csharpLabel

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 16 - 包含先前程式碼產生的表格的輸出文檔

7.2 Microsoft.Office.Interop.Word 的特定進階功能

Microsoft.Office.Interop.Word 允許與 Word 文件深度集成,使開發人員能夠以程式設計方式利用 Word 的豐富功能。 以下是 Microsoft.Office.Interop.Word 提供的一些進階功能,這些功能與前面概述的一般功能有所不同:

7.2.1內容控制

Microsoft.Office.Interop.Word 支援新增和操作內容控件,包括富文本方塊、組合方塊、日期選擇器和複選框。 這些控制項可用於建立結構化和互動式文檔,使用者可以在設定的範本內輸入或選擇資訊。

using Microsoft.Office.Interop.Word;

// Create a new Word application and document
var application = new Application();
var document = application.Documents.Add();

// Add a combo box content control
object missing = System.Reflection.Missing.Value;
var cc = document.ContentControls.Add(WdContentControlType.wdContentControlComboBox, ref missing);
cc.Title = "Choose an option";
cc.DropdownListEntries.Add("Option 1", "1");
cc.DropdownListEntries.Add("Option 2", "2");
using Microsoft.Office.Interop.Word;

// Create a new Word application and document
var application = new Application();
var document = application.Documents.Add();

// Add a combo box content control
object missing = System.Reflection.Missing.Value;
var cc = document.ContentControls.Add(WdContentControlType.wdContentControlComboBox, ref missing);
cc.Title = "Choose an option";
cc.DropdownListEntries.Add("Option 1", "1");
cc.DropdownListEntries.Add("Option 2", "2");
Imports Microsoft.Office.Interop.Word

' Create a new Word application and document
Private application = New Application()
Private document = application.Documents.Add()

' Add a combo box content control
Private missing As Object = System.Reflection.Missing.Value
Private cc = document.ContentControls.Add(WdContentControlType.wdContentControlComboBox, missing)
cc.Title = "Choose an option"
cc.DropdownListEntries.Add("Option 1", "1")
cc.DropdownListEntries.Add("Option 2", "2")
$vbLabelText   $csharpLabel

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 17 - 包含先前程式碼內容控制項的輸出文檔

7.2.2郵件合併

自動執行郵件合併流程,將 Word 文件與電子表格或資料庫等資料來源合併,即可大量產生個人化信函、標籤或電子郵件。此功能對於大量產生客製化通訊或文件至關重要。

using Microsoft.Office.Interop.Word;

// Create a new document for mail merge
var mailMergeDoc = application.Documents.Add();

// Open the data source for the mail merge
mailMergeDoc.MailMerge.OpenDataSource("path\\to\\datasource.xlsx");

// Execute the mail merge
mailMergeDoc.MailMerge.Destination = WdMailMergeDestination.wdSendToNewDocument;
mailMergeDoc.MailMerge.Execute(false);
using Microsoft.Office.Interop.Word;

// Create a new document for mail merge
var mailMergeDoc = application.Documents.Add();

// Open the data source for the mail merge
mailMergeDoc.MailMerge.OpenDataSource("path\\to\\datasource.xlsx");

// Execute the mail merge
mailMergeDoc.MailMerge.Destination = WdMailMergeDestination.wdSendToNewDocument;
mailMergeDoc.MailMerge.Execute(false);
Imports Microsoft.Office.Interop.Word

' Create a new document for mail merge
Private mailMergeDoc = application.Documents.Add()

' Open the data source for the mail merge
mailMergeDoc.MailMerge.OpenDataSource("path\to\datasource.xlsx")

' Execute the mail merge
mailMergeDoc.MailMerge.Destination = WdMailMergeDestination.wdSendToNewDocument
mailMergeDoc.MailMerge.Execute(False)
$vbLabelText   $csharpLabel

7.2.3追蹤更改和註釋

透過程式碼啟用和使用 Word 的追蹤變更和註解功能。 對於需要協作編輯、文件審查或審核一段時間內的變更的應用程式來說,此功能至關重要。

using Microsoft.Office.Interop.Word;

// Enable track changes
document.TrackRevisions = true;

// Add a comment to the first paragraph
object start = 0;
object end = 0;
Range range = document.Paragraphs[1].Range;
document.Comments.Add(range, "This is a comment.");
using Microsoft.Office.Interop.Word;

// Enable track changes
document.TrackRevisions = true;

// Add a comment to the first paragraph
object start = 0;
object end = 0;
Range range = document.Paragraphs[1].Range;
document.Comments.Add(range, "This is a comment.");
Imports Microsoft.Office.Interop.Word

' Enable track changes
document.TrackRevisions = True

' Add a comment to the first paragraph
Dim start As Object = 0
Dim [end] As Object = 0
Dim range As Range = document.Paragraphs(1).Range
document.Comments.Add(range, "This is a comment.")
$vbLabelText   $csharpLabel

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 18 - 輸出文檔,其中包含來自先前程式碼的註釋追蹤

7.2.4頁首和頁尾

動態操作頁首和頁腳,包括新增頁碼、日期或自訂文字。 這對於創建具有一致品牌形象和導航輔助功能的專業文件至關重要。

using Microsoft.Office.Interop.Word;

// Iterate through sections in the document and set headers and footers
foreach (Section section in document.Sections)
{
    // Set text for primary header
    section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "Header Text";

    // Add page numbers to primary footer
    section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add();
}
using Microsoft.Office.Interop.Word;

// Iterate through sections in the document and set headers and footers
foreach (Section section in document.Sections)
{
    // Set text for primary header
    section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "Header Text";

    // Add page numbers to primary footer
    section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add();
}
Imports Microsoft.Office.Interop.Word

' Iterate through sections in the document and set headers and footers
For Each section As Section In document.Sections
	' Set text for primary header
	section.Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = "Header Text"

	' Add page numbers to primary footer
	section.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.Add()
Next section
$vbLabelText   $csharpLabel

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 19 - 包含先前程式碼產生的頁首和頁尾的輸出文檔

7.2.5目錄和索引

根據文件標題或標記條目產生和更新目錄或索引。 這對於創建易於瀏覽的專業文檔,特別是篇幅較長的報告或書籍,至關重要。

using Microsoft.Office.Interop.Word;

// Add a table of contents to the document
var toc = document.TablesOfContents.Add(document.Content, UpperHeadingLevel: 1, LowerHeadingLevel: 3, UseHyperlinks: true);

// Add an index to the document
var index = document.Indexes.Add(document.Content);
using Microsoft.Office.Interop.Word;

// Add a table of contents to the document
var toc = document.TablesOfContents.Add(document.Content, UpperHeadingLevel: 1, LowerHeadingLevel: 3, UseHyperlinks: true);

// Add an index to the document
var index = document.Indexes.Add(document.Content);
Imports Microsoft.Office.Interop.Word

' Add a table of contents to the document
Private toc = document.TablesOfContents.Add(document.Content, UpperHeadingLevel:= 1, LowerHeadingLevel:= 3, UseHyperlinks:= True)

' Add an index to the document
Private index = document.Indexes.Add(document.Content)
$vbLabelText   $csharpLabel

7.2.6嵌入和連結對象

將 Excel 圖表或 PowerPoint 簡報等外部物件嵌入或連結到 Word 文件中。 此功能允許動態內容隨來源檔案更新,或將資訊直接嵌入文件中。

using Microsoft.Office.Interop.Word;

// Embed an Excel sheet into the document
object missing = System.Reflection.Missing.Value;
document.InlineShapes.AddOLEObject(
    ClassType: "Excel.Sheet", 
    FileName: "path\\to\\excel.xlsx", 
    LinkToFile: false, 
    DisplayAsIcon: false, 
    IconFileName: ref missing, 
    IconIndex: ref missing, 
    IconLabel: ref missing, 
    Anchor: ref missing
);
using Microsoft.Office.Interop.Word;

// Embed an Excel sheet into the document
object missing = System.Reflection.Missing.Value;
document.InlineShapes.AddOLEObject(
    ClassType: "Excel.Sheet", 
    FileName: "path\\to\\excel.xlsx", 
    LinkToFile: false, 
    DisplayAsIcon: false, 
    IconFileName: ref missing, 
    IconIndex: ref missing, 
    IconLabel: ref missing, 
    Anchor: ref missing
);
Imports Microsoft.Office.Interop.Word

' Embed an Excel sheet into the document
Private missing As Object = System.Reflection.Missing.Value
document.InlineShapes.AddOLEObject(ClassType:= "Excel.Sheet", FileName:= "path\to\excel.xlsx", LinkToFile:= False, DisplayAsIcon:= False, IconFileName:= missing, IconIndex:= missing, IconLabel:= missing, Anchor:= missing)
$vbLabelText   $csharpLabel

8.文件和支援。

8.1 IronWord 文件和支持

IronWord 提供詳盡的文件和支持,幫助開發者充分發揮其潛力。 文件涵蓋了各種資源,包括詳細的入門指南、功能摘要、快速入門範例和全面的 API 參考。

IronWord 提供強大的支持,可提供技術援助,確保開發人員能夠快速解決任何問題。 支援體系包括一個專門的團隊,負責解答疑問和解決問題。

8.2 Microsoft.Office.Interop.Word 文件和支持

Microsoft.Office.Interop.Word 為 .NET 開發人員提供以程式設計方式與 Word 文件互動的工具,並有微軟豐富的文件和支援作為後盾。 文件內容涵蓋了從入門指南到高級範例、詳細的 API 資訊和實際用例等所有內容。

如需獲得支持,微軟提供多種管道,包括直接技術援助、社群論壇以及 Stack Overflow 等同儕支援平台。 GitHub 對於報告問題和提出改進建議也至關重要。 定期更新確保程式庫與新的 Word 版本保持同步,體現了微軟對開發者需求的持續承諾。

9. 許可模式

9.1 IronWord 許可證

IronWord 與 Microsoft.Office.Interop.Word 的比較:圖 20 - IronWord 授權頁面

IronWord 提供免費和商業許可選項

-精簡版授權:$liteLicense 美元

  • 適用於 1 位開發人員
  • 1 位置
  • 1 個專案
  • 電子郵件支援
    • Plus 許可證:$plusLicense 美元 最多 3 位開發人員
  • 3 個地點
  • 3 個專案
  • 電子郵件(全天候 24/7)和線上聊天支持 電話支援 -專業授權:$professionalLicense 美元 最多 10 位開發人員
  • 10 個地點
  • 10 個專案
  • 電子郵件(全天候 24/7)、聊天和電話支持
  • 螢幕分享支援

所有商業許可證均包含優先支援和更新。 它還提供免費試用版,讓用戶可以在生產環境中測試所有功能,而不會帶有任何浮水印。

9.2 Microsoft.Office.Interop.Word 許可證

Microsoft.Office.Interop.Word 的授權與 Microsoft Office 套件綁定。 Interop 庫本身不需要單獨的許可; 它包含在 Microsoft Office 中。在應用程式中使用 Microsoft.Office.Interop.Word 需要部署環境具有有效的 Microsoft Office 許可證,其中包括 Word。

費用取決於所選的特定 Office 套件版本和授權協議,從個人授權到企業大量授權不等。 如需了解準確價格,請參考微軟官方網站或聯絡微軟銷售代表。

10.結論

在對IronWord和 Microsoft.Office.Interop.Word 進行比較時,我們考察了功能、易用性、平台相容性和授權等各個方面。 對於尋求靈活的跨平台 Word 文件處理解決方案的開發人員來說,IronWord 是一個非常強大的競爭者。

IronWord 能夠獨立於 Microsoft Office 運行,為文件處理提供了一種簡化的方法,使其成為在各種環境下運行的應用程式的理想選擇。 IronWord 具有全面的功能集,而且無需安裝 Microsoft Word,這使其具有優勢,尤其是在部署簡單性和廣泛兼容性是關鍵考慮因素的情況下。 如果您想購買 IronWord,請前往授權頁面

請注意Microsoft.Office.Interop.Word 是其各自所有者的註冊商標。 本網站與 Microsoft.Office.Interop.Word 沒有任何關聯,也未獲得其認可或贊助。 所有產品名稱、標誌和品牌均為其各自擁有者的財產。 比較資料僅供參考,並反映撰寫時的公開資訊。

常見問題解答

如何在不安裝 Microsoft Word 的情況下,在 .NET 中建立、編輯和格式化 Word 文件?

您可以使用 IronWord,它是一個獨立的 .NET 函式庫,可以在不需要安裝 Microsoft Word 的情況下進行文件操作、轉換成各種格式,以及提供全面的格式選項。

Microsoft.Office.Interop.Word 與 IronWord for .NET 專案有何差異?

Microsoft.Office.Interop.Word 需要安裝 Microsoft Word,並提供與 Word 功能的深度整合。IronWord 是獨立的函式庫,不需要 Word,並支援跨平台開發,因此在伺服器或雲端情境下更為靈活。

如何使用 Visual Studio 安裝 Word 文件處理的 .NET 函式庫?

IronWord 和 Microsoft.Office.Interop.Word 均可使用 Visual Studio 中的 NuGet Package Manager、Visual Studio Command Line 或直接從 NuGet 網站下載安裝。

IronWord 有哪些授權選項?

IronWord 提供各種不同的 License 選項,包括免費和商業 License,以及 Lite、Plus 和 Professional 層級,每層級都有不同的開發人員、地點和專案限制。

Microsoft.Office.Interop.Word 如何與 Microsoft Word 功能整合?

Microsoft.Office.Interop.Word 允許 .NET 應用程式利用 Word 的全部功能,包括文件建立、編輯、格式化、內容控制、郵件合併以及其他需要安裝 Microsoft Word 的進階功能。

哪一種函式庫更適合 .NET 的跨平台開發?

IronWord 更適合跨平台開發,因為它不需依賴 Microsoft Word 的安裝,並支援不同作業系統上的應用程式。

IronWord 在文件處理方面提供哪些進階功能?

IronWord 提供進階功能,例如閱讀和編輯 Word 文件、設定頁面、新增段落、圖片、表格,以及套用樣式和對齊方式,而不需要 Microsoft Word。

在 IronWord 和 Microsoft.Office.Interop.Word 之間做選擇的主要考量為何?

考慮您的環境是否已經安裝 Microsoft Word,以及是否需要與 Word 功能深度整合。IronWord 為沒有 Word 的伺服器或雲端應用程式提供彈性,而 Microsoft.Office.Interop.Word 則提供全面的 Word 功能存取,但需要安裝 Word。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。