使用 IRONWORD 如何使用 C# 操作 Word 文档 Jordi Bardia 已更新:六月 22, 2025 Download IronWord NuGet 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Microsoft created Word to serve as a word processor. Initially available under the name Multi-Tool Word for Xenix systems, it was introduced on October 25, 1983. Subsequent versions were developed for a wide range of operating systems, such as SCO Unix (1990), Microsoft Windows (1989), Atari ST (1988), OS/2 (1989), AT&T UNIX PC (1985), IBM PCs running DOS (1983), Apple Macintosh running the Classic macOS (1985), macOS (2001), Web browsers (2010), iOS (2014), and Android (2015). Wine can be used to run older versions of Microsoft Word on Linux. Commercial Word versions can be licensed as a stand-alone application or as a component of Microsoft 365, which can be purchased as a perpetual license or as part of a Microsoft 365 subscription. In this article, we will manipulate Word documents using C# with the help of Microsoft Interop assemblies and explore how IronXL helps us to Edit Excel documents. How To Manipulate Word Document Using C# Make a brand-new Visual Studio project undertaking. Set up the necessary library to read Word documents. To manipulate a Word document, load an existing file or create a new file. Edit the document data and parse the file. Get rid of all object that was created. What is Microsoft Interop Programs written in C# or VB.NET can create or open Word documents (DOC, DOCX, and RTF) with Office Interoperability for Microsoft Word. However, it has a lot of drawbacks when used in projects. We will discuss frequent issues that you may run across when using Microsoft Office Interop (Word Automation) from C# or VB.NET in this article. For example: Every client PC required for word automation needs to be licensed for Microsoft Word. On every client's PC, the same version of Microsoft Word must be installed. Word uses a few megabytes of RAM to load different files and DLLs in the background when automation is utilized. Microsoft Word API is accessed via a COM object. Issues may arise when calling a COM object from managed code, such as type conversions, requiring a COM wrapper, and poor .NET Framework integration. Creating a New Project in Visual Studio It is necessary to launch Visual Studio and create a .NET project before using the Interop library. Visual Studio is compatible with any version, though the most recent is advised. Depending on your needs, you can either develop a project template or an application that resembles Windows Forms. I'll be using the Console Application in this case for simplicity's sake. Configure Project Details Next, provide the location and name of the project. Create a New Project File using the .NET Framework Using the Framework drop-down menu, you can choose a .NET Framework. The Dot.NET Framework 4.7 will be utilized for this project. The next action is to press the "Create" button. After the application has generated the solution, you may input the code and build or run the program by accessing the Program.cs file. Now that the Microsoft.Office.Interop.Word library has been added, we can test the code. Install Interop Library The next repair requires installing the Interop library. Enter the following command in the NuGet Package Manager Console to accomplish this: Install-Package Microsoft.Office.Interop.Word Another way to find the package "Interop" is to use the NuGet Package Manager. Among all the NuGet packages related to Interop, we may select the required package to download from this list. Once you have installed all the necessary libraries, you can then start to edit DOCX files. Manipulate Existing Word Documents using Interop To use Microsoft Word, you must first create an instance of Microsoft.Office.Interop.Word.Application. The communication of Word documents would take place in this instance. The next step is to create a new Word document instance using the Documents property of the Microsoft.Office.Interop.Word.Application instance we just created. As seen in the C# code excerpt below, this allows us to manipulate Word documents programmatically: using System; using Microsoft.Office.Interop.Word; class Program { static void Main() { try { // Create a new instance of Word Application var WordApp = new Microsoft.Office.Interop.Word.Application(); // Open an existing document var WordDoc = WordApp.Documents.Open(@"d:/Demo.docx"); // Edit the content of the first paragraph WordDoc.Paragraphs[1].Range.Text = "New text here..."; // Save the edited document WordDoc.SaveAs(@"d:/NewDemo.docx"); // Close the document WordDoc.Close(); // Quit the Word application WordApp.Quit(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } using System; using Microsoft.Office.Interop.Word; class Program { static void Main() { try { // Create a new instance of Word Application var WordApp = new Microsoft.Office.Interop.Word.Application(); // Open an existing document var WordDoc = WordApp.Documents.Open(@"d:/Demo.docx"); // Edit the content of the first paragraph WordDoc.Paragraphs[1].Range.Text = "New text here..."; // Save the edited document WordDoc.SaveAs(@"d:/NewDemo.docx"); // Close the document WordDoc.Close(); // Quit the Word application WordApp.Quit(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } Imports System Imports Microsoft.Office.Interop.Word Friend Class Program Shared Sub Main() Try ' Create a new instance of Word Application Dim WordApp = New Microsoft.Office.Interop.Word.Application() ' Open an existing document Dim WordDoc = WordApp.Documents.Open("d:/Demo.docx") ' Edit the content of the first paragraph WordDoc.Paragraphs(1).Range.Text = "New text here..." ' Save the edited document WordDoc.SaveAs("d:/NewDemo.docx") ' Close the document WordDoc.Close() ' Quit the Word application WordApp.Quit() Catch ex As Exception Console.WriteLine(ex.ToString()) End Try End Sub End Class $vbLabelText $csharpLabel In the above code, we are able to edit the Word document in C#. First, we create an instance of the Word application using Interop. The Open method is then used to open an existing Word file, converting it into a document object. We can then access the various properties and methods available for interacting with the document. In the example, we update the text of the first paragraph, using the Paragraphs collection and an index to specify which paragraph to edit. Finally, the changes are saved with SaveAs, and the document and application are properly closed. IronXL Library Alternative to Interop IronXL is an alternative to Microsoft Interop that may be used in .NET programs to handle Excel files. While Microsoft Interop requires interacting with Excel through the Interop assemblies, IronXL offers a more straightforward, effective, and powerful method for programmatically manipulating Excel files in .NET contexts. Utilizing IronXL instead of Microsoft Interop has several benefits, such as: Performance and Resource Efficiency: Compared to Microsoft Interop, which depends on the Excel application being installed on the computer, IronXL performs better and uses fewer resources because it is not dependent on the Excel application. Ease of Use and Simplicity: IronXL provides an easier-to-use API that simplifies the reading, writing, and manipulating of Excel files without the complications that come with Microsoft Interop. Compatibility and Dependency: IronXL eliminates dependencies and compatibility problems that may occur with various versions of Excel or Office by not requiring the installation of Microsoft Excel on the computer. Platform Independence: Unlike Microsoft Interop, which may be more closely associated with particular Microsoft Office versions, IronXL offers greater flexibility and ease of deployment across various environments and platforms. For .NET developers who must operate with Excel files programmatically, IronXL is frequently a better option because of its ease of use, speed, and reduced reliance on third-party software installations. The decision between IronXL and Microsoft Interop, however, could be influenced by the specifics of the project, the infrastructure that already exists, and the user's level of expertise with each library. When deciding between these options, always keep your application's requirements in mind. Check out this link to learn more about the IronXL library. Installing IronXL Library Since the IronXL library is needed for the upcoming patch, install it. To finish this, open the NuGet Package Manager Console and type the following command: Install-Package IronWord Searching for the package "IronXL" via the NuGet Package Manager is an additional choice. From this list of every NuGet package linked to IronXL, we can select the one we need to download. Editing Excel Documents using IronXL Data can be exported to the XLSX or XLS formats with just a few lines of code. The following example of source code shows how data can be exported from an Excel file into a simple tabular table format: using IronXL; class Program { static void Main() { // Load an existing Excel file var workbook = WorkBook.Load("Demo file.xlsx"); // Access the first sheet or the sheet by name var ws = workbook.GetWorkSheet("Sheet1"); // Read a value from a cell and output it to the console string address_val = ws["A1"].ToString(); Console.WriteLine(address_val); // Modify a cell's value ws["A2"].Value = "Hello World"; // Save the workbook to different formats workbook.SaveAs("export.xlsx"); workbook.SaveAs("export.xls"); workbook.WorkSheets[0].SaveAs("export.xls"); } } using IronXL; class Program { static void Main() { // Load an existing Excel file var workbook = WorkBook.Load("Demo file.xlsx"); // Access the first sheet or the sheet by name var ws = workbook.GetWorkSheet("Sheet1"); // Read a value from a cell and output it to the console string address_val = ws["A1"].ToString(); Console.WriteLine(address_val); // Modify a cell's value ws["A2"].Value = "Hello World"; // Save the workbook to different formats workbook.SaveAs("export.xlsx"); workbook.SaveAs("export.xls"); workbook.WorkSheets[0].SaveAs("export.xls"); } } Imports IronXL Friend Class Program Shared Sub Main() ' Load an existing Excel file Dim workbook = WorkBook.Load("Demo file.xlsx") ' Access the first sheet or the sheet by name Dim ws = workbook.GetWorkSheet("Sheet1") ' Read a value from a cell and output it to the console Dim address_val As String = ws("A1").ToString() Console.WriteLine(address_val) ' Modify a cell's value ws("A2").Value = "Hello World" ' Save the workbook to different formats workbook.SaveAs("export.xlsx") workbook.SaveAs("export.xls") workbook.WorkSheets(0).SaveAs("export.xls") End Sub End Class $vbLabelText $csharpLabel The previous example loads an Excel file that already exists by calling the Load function, which takes an argument for the file path and name. Importing the file into the WorkBook object is now complete. The Excel worksheets are then loaded with the help of GetWorkSheet, which allows us to load the worksheet using the sheet name. The Excel address was then used to read the value. To know more about reading Excel files click here. We can alter the Excel sheet's values by utilizing the same Excel address. The Excel document can be saved as an XLSX or XLS file by utilizing the SaveAs function that is offered by the WorkBook object. Using this process, the entire file is saved in the chosen format. Additionally, we can choose a specific Excel worksheet by utilizing its index value or by referring to it by name. Next, we may export the data from the Excel spreadsheet to a different file by using the SaveAs option. Click this link to find out more about formatting and exporting Excel files. Conclusion One of the most popular add-ons for Excel is IronXL. It doesn't rely on any additional external libraries. It is not necessary to install Microsoft Excel because it is self-contained. It operates via a multitude of channels. This contrasts with the Interop library which has to parse the file using extra libraries to edit Word documents. A complete solution for any programming process utilizing Microsoft Excel documents is IronXL. Calculations, sorting strings or numbers, pruning, adding, finding and replacing, merging and unmerging, and file storage are just a few of the many available operations. Not only can spreadsheet data be validated, but you can also construct new forms of cell data. It facilitates reading and writing files as well as handling Excel data. When IronXL was first released, it cost $799. Alternatively, customers can opt to pay a one-year subscription fee to receive software updates and support. For a charge, IronXL provides security against unauthorized redistribution. Go to the IronXL licensing page. To know more about Iron Software products check here. 常见问题解答 我如何使用 C# 操作 Word 文档? 要使用 C# 操作 Word 文档,可以使用 Microsoft.Office.Interop.Word 库。这涉及创建 Word 应用程序的实例,打开文档,进行修改,并以编程方式保存文档。 使用 Microsoft Interop 操作 Word 文档的限制是什么? 使用 Microsoft Interop 的限制包括要求每台客户端 PC 安装有许可证的 Microsoft Word,可能出现版本兼容性问题,以及由于后台进程而导致的内存消耗增加。 如何在 Visual Studio 中设置一个 C# 项目以处理 Word 文档? 在 Visual Studio 中,可以通过选择控制台应用程序设置一个新项目,配置必要的项目详细信息,并确保选择正确的 .NET Framework 版本。然后需要通过 NuGet 包管理器添加对 Microsoft.Office.Interop.Word 的引用。 IronXL 和 Microsoft Interop 在处理 Excel 文件方面有什么区别? IronXL 相对于 Microsoft Interop 提供了优势,如不需要安装 Excel,提供更好的性能,并拥有更简单的 Excel 文件操作 API。它还消除了与 Interop 方法相关的兼容性问题。 我如何在我的.NET项目中安装IronXL? 要在您的 .NET 项目中安装 IronXL,请在 Visual Studio 中打开 NuGet 包管理器控制台并执行命令 Install-Package IronXL.Excel。您也可以在 NuGet 包管理器 UI 中搜索 IronXL 并直接安装。 如何在 C# 中使用 IronXL 编辑 Excel 文档? 使用 IronXL,您可以通过 WorkBook.Load 加载 Excel 文档,访问特定工作表,修改单元格值,并使用 WorkBook 和 WorkSheet 对象提供的方法保存工作簿。 使用 IronXL 操作 Excel 文件有什么好处? IronXL 提供的优势包括提高的性能、易用性和平台独立性。它不需要安装 Excel,从而消除了依赖问题,并允许无缝集成到 .NET 应用程序中。 我可以在不使用 Microsoft Interop 的情况下自动化 Word 文档任务吗? 可以的,各种第三方库提供了 Microsoft Interop 的替代方案用于自动化 Word 文档任务,提供更简单的 API,并消除了安装 Microsoft Word 的需要。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 相关文章 已更新九月 18, 2025 ASP .NET Core 导入和导出 Word 文件 本指南探讨了如何使用 IronWord 库导入现有的 Word 文档、显示其内容并从头创建文档 阅读更多 已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多 已更新六月 22, 2025 如何使用 C# 在 Word 中对齐文本 深入了解 IronWord NuGet 包以及使用该包对齐文本或段落的方法 阅读更多 如何使用 C# 读取 Word 文件如何在 C# 中创建 Word 文档
已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多