产品比较 IronWord 与 Microsoft.Office.Interop.Word 的比较 Jordi Bardia 已更新:八月 4, 2025 下载 IronWord NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 1.简介 许多开发人员使用 .NET 应用程序,他们需要处理 Word 文档。 这可能是用于创建报告、处理文档或生成发票。 用于此的两个流行工具是 Microsoft.Office.Interop.Word 和 IronWord。 每个工具都有其自己的功能集、优点和缺点。 本文将比较 Microsoft.Office.Interop.Word 和 IronWord。 我们将看看每个工具提供了什么,它们的性能如何,以及在什么场景中最好使用它们。 这应该有助于您决定哪个工具更适合您的特定需求。 2. 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 是一个为文档操作设计的 .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 项目的轻松集成: 考虑到 .NET 应用程序而设计,IronWord 可以通过 NuGet 轻松集成到任何 .NET 项目中。 其 API 设计对于 .NET 开发人员来说直观,使得开始与 Word 文档一起工作变得简单明了。 跨平台兼容性: IronWord 支持跨平台开发,使其可用于目标是各种操作系统的应用程序。 这对于需要在 Windows 操作系统和 Linux 操作系统上运行或在云环境中运行的项目特别有价值。 4. 在 Visual Studio 中创建 .NET 项目 4.1 在 Visual Studio 中打开 首先启动 Microsoft Visual Studio。 打开应用程序后,您将看到启动窗口。 此窗口提供了启动的各种选项,包括打开现有项目或创建新项目。 为了我们的目的,您将专注于启动新项目。 4.2 选择项目类型 点击创建新项目的选项后,Visual Studio 会为您提供模板列表。 要继续,选择控制台应用项目。 4.3 配置项目 一旦选择了所需的项目模板,点击"下一步"会带您进入项目配置界面。 在这里,您需要提供有关项目的基本信息。 这包括指定项目名称并选择存放项目文件的路径,如应用程序内部使用的程序文件文件夹。 下一个对话框涉及选择目标框架。 您选择的 .NET 框架决定了应用程序可用的库和 API,从而影响其兼容性和功能。 确保选择一个支持您计划在应用程序中实现的功能的框架版本,利用 Visual Studio 工具提供的优化开发体验。 4.4 创建项目 填写必要的信息并确保所有设置已调整正确以满足项目需求后,最后一步是创建项目。 通过点击"创建"按钮实现。 Visual Studio 然后初始化一个基于所选择的模板和配置的新项目。 这个过程可能需要几分钟,之后您的新 .NET 项目将打开并准备好进行开发。 5. 安装 IronWord 库 本节讨论如何使用 NuGet 包管理器、Visual Studio 命令行以及直接从 NuGet 网页下载,将 IronWord 库融入到您的项目中。 5.1 使用 NuGet 软件包管理器 NuGet 包管理器是 Visual Studio 内的一个用户友好界面,允许无缝地安装库和工具。 要使用此方法安装 IronWord 库,请按照以下步骤操作: 打开 NuGet 包管理器:在项目打开的状态下,在解决方案资源管理器中右键点击项目名称,并从上下文菜单中选择"管理 NuGet 包..." 此操作会打开 NuGet 包管理器窗口。 搜索 IronWord:在 NuGet 包管理器窗口中,导航到"浏览"选项卡。 使用搜索框找到 IronWord 库,输入"IronWord"并按回车键。 安装 IronWord:从搜索结果中选择 IronWord 包。 您将看到一个详细的描述和版本信息。 选择适合您项目需求的版本并点击"安装"按钮。 Visual Studio 然后会处理库的下载和安装,相应地更新您的项目引用。 5.2 使用Visual Studio命令行 对于那些喜欢使用命令行工具的人来说,Visual Studio 提供了包管理器控制台,一个用于管理 NuGet 包的 PowerShell 界面。 访问包管理器控制台:在 Visual Studio 主菜单中,导航至"工具">"NuGet 包管理器">"包管理器控制台"。 安装 IronWord:在控制台中输入以下命令并按回车键: Install-Package IronWord 该命令会获取最新版本的 IronWord 库并将其集成到您的项目中。 您可以通过添加 -Version 参数并跟随版本号来指定特定版本。 5.3从 NuGet 网页直接下载 如果您更喜欢手动管理项目库,您可以直接从 NuGet 网站下载 IronWord 包并将其添加到您的项目中。 下载包:访问 nuget.org 并搜索 IronWord 包。 在包页面上,找到"下载"链接以获取.nupkg 文件。将此文件保存到您的电脑。 添加到项目中:下载后,您可以手动将包添加到您的项目中。 该过程通常涉及提取 .nupkg 文件,然后在您的项目中添加对所包含程序集的引用。 但是,通常使用 NuGet 包管理器或 Visual Studio 命令行会更简单且不易出错。 6. 安装 Microsoft.Office.Interop.Word 可以通过多种方法将此库添加到您的项目中,每种方法都适合不同的开发偏好和要求。 6.1 使用 NuGet 包管理器 Visual Studio 中的 NuGet 包管理器 是将 Microsoft.Office.Interop.Word 库添加到您项目的最直接方法。 请遵循以下步骤: 打开 NuGet 包管理器:在 Visual Studio 中打开项目时,在解决方案资源管理器中右键单击项目名称。 从上下文菜单中选择"管理 NuGet 包..."以打开 NuGet 包管理器窗口。 搜索库:在 NuGet 包管理器窗口中,切换到"浏览"选项卡并在搜索框中输入"Microsoft.Office.Interop.Word"。 按下回车键以启动搜索。 安装包:在搜索结果中找到"Microsoft.Office.Interop.Word"包。 查看提供的版本信息和描述,以确保其满足您项目的需求。 点击"安装"按钮将库添加到您项目中。 Visual Studio 会处理包的下载和安装,更新项目引用以包含必要的程序集。 6.2 使用 Visual Studio 命令行 Visual Studio 内的命令行界面包管理器控制台,还提供了安装 Microsoft.Office.Interop.Word 库的另一种方式。 访问包管理器控制台:在 Visual Studio 主菜单中,转到"工具">"NuGet 包管理器">"包管理器控制台"。 执行安装命令:在控制台中,输入以下命令并按下回车键: Install-Package Microsoft.Office.Interop.Word 该命令指示 Visual Studio 获取该库的最新版本并将其整合到您项目中。 如果您需要特定版本的库,您可以通过附加 -Version 参数并紧接所需版本号来指定。 6.3 从 NuGet 网页直接下载 对于偏好手动管理项目依赖的开发人员,可以直接从 NuGet 网站下载 Microsoft.Office.Interop.Word 库。 下载包:导航到 nuget.org 并搜索"Microsoft.Office.Interop.Word"。 在包的页面上,找到"下载"链接以获取 .nupkg 文件。将此文件保存到您计算机上的已知位置。 手动将包添加到您的项目:下载后,可以手动将 .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 7.1.2 编辑页面设置 IronWord 能够更改文档的纸张大小,适应如 A4、信纸或法律标准。 您可以根据文档的要求调整页面方向至纵向和横向。 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 7.1.4 添加与删除文本运行 文本运行表示具有一组共有属性的文本段。 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 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 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 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 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 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 嵌入和链接对象 在 Word 文档中嵌入或链接外部对象,如 Excel 图表或 PowerPoint 演示文稿。 此功能允许动态内容随源文件更新或将信息直接嵌入文档中。 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 提供了免费的和商业的许可选项。 Lite 许可证:$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 套件版本和选择的许可协议,从个人许可证到企业批量许可不等。 如需精确的定价,请参见 Microsoft 的官方网站或联系 Microsoft 销售代表。 10. 结论 在IronWord与 Microsoft.Office.Interop.Word 的比较中,我们检查了功能、易用性、平台兼容性和许可等各个方面。 IronWord 对于寻找一个灵活、跨平台的 Word 文档操作解决方案的开发者来说,显得尤其优秀。 凭借其独立于 Microsoft Office 运行的能力,IronWord 提供了一种简化的文档处理方法,使其成为在各种环境中运行应用程序的理想选择。 其全面的功能集,加上不需要安装 Microsoft Word 的便利,使 IronWord 更具优势,尤其在部署简单性和广泛兼容性是关键考虑因素的场景中。 如果您想购买 IronWord,请访问许可页。 请注意Microsoft.Office.Interop.Word 是其各自所有者的注册商标。 本网站与 Microsoft.Office.Interop.Word 无关联,不受其认可或赞助。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映的是撰写时的公开信息。 常见问题解答 如何在.NET 中创建、编辑和格式化 Word 文档,而无需安装 Microsoft Word? 您可以使用 IronWord,这是一个独立的.NET库,允许文档操作、转换为各种格式,并提供全面的格式选项,无需安装 Microsoft Word。 Microsoft.Office.Interop.Word 和用于.NET项目的 IronWord 有什么区别? Microsoft.Office.Interop.Word 需要安装 Microsoft Word,并提供与 Word 功能的深度集成。而 IronWord 是一个独立的库,不需要 Word,并支持跨平台开发,使其在服务器或云环境中更具灵活性。 如何使用 Visual Studio 安装用于 Word 文档处理的 .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 为服务器或云应用程序提供了灵活性,并且不需要 Word,而Microsoft.Office.Interop.Word可以提供全面的Word功能访问但需要Word安装。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 相关文章 已更新七月 28, 2025 IronWord 与 Aspose Words 的 .NET 比较 将 Aspose.Word 视为您工具库中的多功能工具,能够处理多种文档格式,并为复杂的文档创建和操作提供广泛的功能。 阅读更多 IronWord 与 Aspose Words 的 .NET ...
已更新七月 28, 2025 IronWord 与 Aspose Words 的 .NET 比较 将 Aspose.Word 视为您工具库中的多功能工具,能够处理多种文档格式,并为复杂的文档创建和操作提供广泛的功能。 阅读更多