跳至頁尾內容
產品比較

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

1. 介紹

許多開發者使用.NET應用程式來處理Word文件。 這可能是為了建立報告、處理文件或生成發票。 兩個受歡迎的工具是Microsoft.Office.Interop.Word和IronWord。 每個工具都有自己的功能、優點和缺點。

This article will compare Microsoft.Office.Interop.Word and IronWord. 我們將探討每個工具的提供功能、性能以及它們在哪些情況下最佳使用。 這應能幫助您決定哪個工具更適合您的特定需求。

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

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和純文字。 該功能對於需要以多種格式展示文件或需要在Web應用程式中顯示文件內容的應用程式至關重要。

全面的格式化選項:

IronWord提供了廣泛的格式化能力,允許開發者應用文字樣式,新增段落對齊,插入表格和圖片等元素。 這些選項確保IronWord生成或修改的文件符合理想的視覺和結構標準。

輕鬆整合到.NET專案:

專為.NET應用程式設計,IronWord可以通過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框架決定了應用程式可用的程式庫和API,影響其相容性和功能。 確保選擇一個支持您計劃在應用程式中實現功能的框架版本,利用Visual Studio工具獲得最佳開發經驗。

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

4.4 建立專案

在填寫必要的資訊並確保所有設置正確調整以滿足您的專案要求後,最終步驟是建立專案。 這是通過點擊"建立"按鈕來實現的。 隨後,Visual Studio將根據選定的範本和配置初始化一個新專案。 該過程可能需要幾分鐘時間,然後您的新.NET專案將打開並準備好進行開發。

5. 安裝 IronWord 程式庫

本節深入探討如何通過NuGet Package Manager、Visual Studio命令行和直接從NuGet網頁下載將IronWord程式庫整合到專案中。

5.1 使用 NuGet Package Manager

NuGet Package Manager是Visual Studio內的使用者友好介面,允許無縫安裝程式庫和工具。 要使用此方法安裝IronWord程式庫,請按照以下步驟操作:

  1. 打開 NuGet Package Manager: 打開專案後,在方案總管中右鍵單擊專案名稱,然後從上下文選單中選擇"管理NuGet Package..." 此操作會打開NuGet Package Manager窗口。
  2. 搜尋 IronWord: 在NuGet Package Manager窗口中,導航到"瀏覽"選項卡。 使用搜索框查找IronWord程式庫,輸入"IronWord"並按Enter。

IronWord與Microsoft.Office.Interop.Word之間的比較:圖7 - 使用NuGet程式包管理器安裝IronWord

  1. 安裝 IronWord: 從搜索結果中選擇IronWord程式包。 您將看到詳細的描述和版本資訊。 選擇適合您專案要求的版本,然後單擊"安裝"按鈕。 隨後,Visual Studio會處理程式庫的下載和安裝,並相應地更新您的專案引用。

5.2 使用 Visual Studio 命令行

對於喜歡使用命令行工具的人,Visual Studio提供Package Manager Console,一個用於管理NuGet程式包的PowerShell介面。

  1. 存取 Package Manager Console: 在Visual Studio的主選單中,導航到"工具" > "NuGet Package Manager" > "Package Manager Console"。
  2. 安裝 IronWord: 在控制台中,輸入以下命令並按Enter:

    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 Package Manager或Visual Studio命令行的方式更簡單且更不易出錯,如上所述。

6. Install Microsoft.Office.Interop.Word

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

6.1 使用 NuGet Package Manager

IronWord與Microsoft.Office.Interop.Word之間的比較:圖10 - 通過NuGet程式包管理器安裝Microsoft.Office.Interop.Word

Visual Studio中的NuGet Package Manager是將Microsoft.Office.Interop.Word程式庫新增到您的專案中的最簡單方法。 請按照以下步驟操作:

  1. 打開 NuGet Package Manager: 在Visual Studio中打開專案後,右鍵單擊方案總管中的專案名稱。 從上下文選單中選擇"管理NuGet Package..."以打開NuGet Package Manager窗口。
  2. 搜尋程式庫: 在NuGet Package Manager窗口中,切換到"瀏覽"標籤,並在搜索框中鍵入"Microsoft.Office.Interop.Word"。 按Enter鍵以開始搜索。
  3. 安裝程式包: 在搜索結果中找到"Microsoft.Office.Interop.Word"程式包。 查看提供的版本資訊和描述以確保其符合您的專案需求。 點擊"安裝"按鈕將程式庫新增到您的專案中。 Visual Studio會處理程式包的下載和安裝,並更新您的專案引用以包含所需的組件。

6.2 使用 Visual Studio 命令行

IronWord與Microsoft.Office.Interop.Word之間的比較:圖11 - 通過VS命令行安裝Microsoft.Office.Interop.Word

Visual Studio中的Package Manager Console,一個命令行介面,提供通過命令行安裝Microsoft.Office.Interop.Word程式庫的另一種方法。

  1. 存取 Package Manager Console: 在Visual Studio的主選單中,導航至"工具">"NuGet Package Manager">"Package Manager Console"。
  2. 執行安裝命令: 在控制台中,輸入以下命令並按Enter:

    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 Package Manager或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

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文件互動的工具,得到了Microsoft詳細文件和支持的支持。 這些文件涵蓋了從初學者指南到高級範例、詳細API資訊和實際使用案例的所有內容。

為了提供支持,Microsoft提供了多種渠道,包括直接技術支援、社區論壇和像Stack Overflow這樣的平台以獲得同行支持。 GitHub對於問題報告和建議改進也是至關重要的。 定期更新確保該程式庫對應新版本Word的更新,反映了Microsoft持續回答開發者需求的承諾。

9. 授權模式

9.1 IronWord授權

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

IronWord提供免費和商業授權選項

  • Lite License: $liteLicense USD
    • 給1位開發人員
    • 1個地點
    • 1個專案
    • 電子郵件支持
  • Plus License: $plusLicense USD
    • 最多3位開發人員
    • 3個地點
    • 3個專案
    • 電子郵件(24/7)和聊天支持
    • 電話支持
  • Professional License: $professionalLicense USD
    • 最多10位開發人員
    • 10個地點
    • 10個專案
    • 電子郵件(24/7)、聊天和電話支持
    • 螢幕分享支持

所有的商業授權都包括優先支持和更新。 它還提供免費試用,以在生產環境中測試所有功能而不影響水印。

9.2 Microsoft.Office.Interop.Word License

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

成本依賴於選擇的特定Office套件版本和授權協議,從個人授權到企業的大量授權不等。 有關精確定價,請參考Microsoft的官方網站或與Microsoft銷售代表聯繫。

10. 結論

IronWord和Microsoft.Office.Interop.Word的比較中,我們檢查了多個方面,如功能、易用性、平台相容性和授權。 IronWord作為一個特別強大的競爭者 ,對於尋求靈活、跨平台解決方案的開發者來說是非常值得推薦的。

憑藉其不依賴於Microsoft Office的操作能力,IronWord提供了一種精簡的文件處理方法,這使其成為在多元環境中運行的應用程式的理想選擇。 它全面的功能集合,加上不需要安裝Microsoft Word的便利性,使IronWord在場景中具備優勢,尤其在考慮簡單部署和廣泛相容性時。 如果您想購買IronWord,請前往授權頁面

Microsoft.Office.Interop.Word是其各自所有者的註冊商標。 本網站未與Microsoft.Office.Interop.Word相關聯、未獲其認可或贊助。 所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供資訊目的,反映了撰寫時的公開可用資訊。)}]

常見問題

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

您可以使用IronWord,一個獨立的.NET程式庫,允許文件操作、轉換為各種格式和全面的格式選擇,而不需要安裝Microsoft Word。

Microsoft.Office.Interop.Word和IronWord的.NET專案之間有什麼區別?

Microsoft.Office.Interop.Word需要安裝Microsoft Word並提供與Word功能的深度整合。IronWord是一個獨立的程式庫,不需要Word,並支持跨平台開發,使其在伺服器或雲端環境中更加靈活。

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

IronWord和Microsoft.Office.Interop.Word都可以使用Visual Studio中的NuGet包管理器、Visual Studio命令行或直接從NuGet網站下載安裝。

IronWord有哪些授權選擇?

IronWord提供多種授權選擇,包括免費和商業授權,有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為伺服器或雲端應用程式提供靈活性,而Microsoft.Office.Interop.Word提供完整的Word功能存取,但需安裝Word。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話