使用 IRONWORD 如何在 C# 中創建 DOCX 文件 Jordi Bardia 更新日期:6月 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 Many applications in today's digital environment require the ability to generate and alter documents automatically. The ability to create and work with DOCX files is useful whether you're building an enterprise-level reporting system, a document management system, or just automating repetitive activities. In this tutorial, we'll dive into the world of C# document generation and examine the robust libraries and programming methods that can be used in C# to create DOCX files. How to Create DOCX File in C# Install the NuGet package IronWord. Take the required namespaces and import them from IronWord. To generate document files, add a new TextRun. Make a new Body and Document. Fill the Body with content (paragraphs, tables, etc.). Using WordDocument, create a new WordDocument. Assigning the Document object to the TextRun, Paragraph Document property will save the modifications. Getting to Know the DOCX Format Before delving into the coding, let us clarify what a DOCX file is. Microsoft Word and other Word processing programs save documents in the DOCX file format. It is based on the ZIP-compressed XML-based [Office Open XML (OOXML) standard](https://en.wikipedia.org/wiki/Office_Open_XML#:~:text=Office%20Open%20XML%20(also%20informally,initial%20version%20as%20ECMA%2D376.). This allows utilizing APIs from Microsoft and other developers to manipulate it programmatically relatively simply. IronWord Iron Software created the Word document library known as IronWord. When it comes to offering strong capabilities for Word document manipulation in .NET applications, IronWord shines. To help .NET C# developers include the ability to create, read, and modify Word and DOCX documents in their programs, IronWord is a robust Word document library. Features of IronWord Create and edit DOC and DOCX: IronWord eliminates the requirement for Office Interop or Microsoft Word installation by enabling you to create and edit Word documents right within your .NET projects. Open, Edit an existing document, and Save a Word file and Docx: The Word document structure is customizable, including page configuration (background color, margins, paper size, and orientation). Include Words and Pictures: With the simple API, you can quickly add text and graphics to your Word documents. Cross-Platform Support: IronWord is compatible with .NET 8, 7, 6, Core, Standard, or Framework, and is built for C#, F#, and VB.NET. It functions in several environments, such as Docker, iOS, Android, Windows, and Linux. Worldwide, millions of engineers rely on us: IronWord is a component of the Iron Suite, which consists of nine .NET API office document products. Developers use it all over the world to create, view, and modify Word documents. To know more about IronWord refer here. Creating a New Project in Visual Studio To launch the Visual Studio application, use the File menu and choose File. Click "New Project," then choose "Console application." Enter the project name in the designated text field after choosing the file location. Next, as seen in the sample below, click the Create button and choose the required .NET Framework. The Visual Studio project's organization will then depend on the selected application. To add code and construct the application, just open the program.cs file. The internet application, Windows, or console can all be used. After that, the library may be added and the code tested. Install IronWord library Making Use of Visual Studio Tool Select NuGet Package Manager from the Tools Menu. Enter the Package Manager interface to gain access to the package management terminal console. Install-Package IronWord After downloading and installing the package, it can now be utilized in the ongoing project. There's also the Package Manager method. Installing the package straight into the solution is possible with Visual Studio's NuGet Package Manager option. You can see how to launch the NuGet Package Manager in the image below. Use the NuGet website's search box to locate packages. All we need to do is look up "IronWord" in the package manager, as the screenshot below illustrates: The list of relevant search results is displayed in the image above. To install the software on your machine, simply adjust these settings. Create DOCX Using IronWord Instantiate a WordDocument object to start a new Word document. To initialize the document, you can use any object that makes up the document structure, like a paragraph, table, or section. Here's an easy illustration in the following code snippet: using IronWord; using IronWord.Models; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { // Create a text run, which is a portion of the document's text content. TextRun textRun = new TextRun("Hello, IronWord!"); // Create a paragraph and add the text run to it. Paragraph paragraph = new Paragraph(); paragraph.AddTextRun(textRun); // Create a new Word document with the paragraph. WordDocument doc = new WordDocument(paragraph); // Export and save the DOCX file to the specified path. doc.SaveAs("document.docx"); } } } using IronWord; using IronWord.Models; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { // Create a text run, which is a portion of the document's text content. TextRun textRun = new TextRun("Hello, IronWord!"); // Create a paragraph and add the text run to it. Paragraph paragraph = new Paragraph(); paragraph.AddTextRun(textRun); // Create a new Word document with the paragraph. WordDocument doc = new WordDocument(paragraph); // Export and save the DOCX file to the specified path. doc.SaveAs("document.docx"); } } } Imports IronWord Imports IronWord.Models Namespace ConsoleApp1 Friend Class Program Shared Sub Main(ByVal args() As String) ' Create a text run, which is a portion of the document's text content. Dim textRun As New TextRun("Hello, IronWord!") ' Create a paragraph and add the text run to it. Dim paragraph As New Paragraph() paragraph.AddTextRun(textRun) ' Create a new Word document with the paragraph. Dim doc As New WordDocument(paragraph) ' Export and save the DOCX file to the specified path. doc.SaveAs("document.docx") End Sub End Class End Namespace $vbLabelText $csharpLabel We can add text to the document with the help of the TextRun class, which allows us to pass the text into the paragraph object with the help of the AddTextRun method. Then we are creating a new WordDocument object to create a new DOCX file. Then, by using the SaveAs method, we can save the new DOCX document in the desired location. Below is the output generated from the above code. To know more about IronWord code refer here. Conclusion We've looked at how to use C# to generate a DOCX document file in this guide. The fundamentals of the DOCX format have been discussed, along with the Open XML SDK and the DocX library, and examples of how to use them to generate, edit, and save DOCX files have been given. All of these tools and methods will put you in a strong position to handle a variety of document creation tasks in your C# projects. Learning document production in C# is a useful ability that will help you in various situations, whether you're automating report generation, developing document management systems, or just making templates for end users. For developers working with .NET C#, IronWord is a powerful and adaptable Word document library. It enables developers to read, write, and edit Word (DOCX) documents in their apps with ease. IronWord's user-friendly API makes processing Word documents easier. Without requiring Microsoft Word to be installed or depending on Office Interop, developers may create and edit Word files directly within their .NET projects. Adding text, paragraphs, and other elements to new Word documents is made simple. IronWord is $749 when it first comes out. In addition, customers who pay a one-year membership fee can receive assistance and updates on products. For a fee, IronWord offers a defense against unrestrained redistribution. To find out more about the approximate cost, go to this page. View more about Iron Software by clicking this link. 常見問題解答 如何使用 C# 生成 DOCX 文件? 要使用 C# 生成 DOCX 文件,可以利用 IronWord 函式庫。首先安裝 IronWord NuGet 套件,匯入所需的命名空間,並啟動一個 WordDocument 對象。然後可以使用如 AddTextRun 等方法添加內容,並使用 SaveAs 方法保存文件。 使用 IronWord 生成 DOCX 文件的優勢是什麼? 在生成 DOCX 文件時,IronWord 提供了若干優勢,包括能夠無需 Microsoft Word 創建和編輯文檔、跨平台支持,以及一個用戶友好的 API,簡化了文檔創建過程。 我可以使用 IronWord 向 DOCX 文件中添加圖片嗎? 是的,IronWord 允許您向 DOCX 文件中添加圖片。可以使用 AddImage 方法將圖片融入到文檔中,這能增強 DOCX 檔案的視覺吸引力和功能性。 IronWord 能編輯現有的 DOCX 文件嗎? 是的,IronWord 使您能夠編輯現有的 DOCX 文件。可以使用 IronWord 開啟一個 DOCX 文件,修改其內容、結構或格式,然後保存更改。 IronWord 是否支持在 Linux 上進行文檔生成? 是的,IronWord 支持在 Linux 上進行文檔生成,作為其跨平台功能的一部分,允許開發者在各種操作系統上創建和操作 DOCX 文件,包括 Linux。 在 C# 項目中設置 IronWord 涉及哪些步驟? 在 C# 項目中設置 IronWord 涉及通過 NuGet 套件管理器安裝 IronWord NuGet 套件,將必要的命名空間匯入到代碼中,並利用 IronWord 的 API 執行文檔創建和編輯任務。 IronWord 如何處理 DOCX 文件的兼容性? IronWord 被設計為與 DOCX 格式兼容,確保使用 IronWord 創建或編輯的文件可以在 Microsoft Word 和其他兼容的文字處理器中開啟和使用。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新日期 9月 18, 2025 ASP .NET Core 導入和導出 Word 文件 本指南探討如何使用 IronWord 庫導入現有的 Word 文件,顯示其內容,並從頭開始創建文件 閱讀更多 更新日期 7月 28, 2025 VS 2022 程式化創建新 Word 文件(教程) 在今天的教程中,我將簡單解釋如何使用 IronWord 程式化創建 Microsoft Word 文檔,並提供簡單範例。 閱讀更多 更新日期 6月 22, 2025 如何使用 C# 對齊 Word 中的文本 讓我們深入了解 IronWord NuGet 包,了解如何使用此包對齊文本或段落 閱讀更多 如何執行 C# Word 自動化如何在 VB .NET 中程式化創...
更新日期 7月 28, 2025 VS 2022 程式化創建新 Word 文件(教程) 在今天的教程中,我將簡單解釋如何使用 IronWord 程式化創建 Microsoft Word 文檔,並提供簡單範例。 閱讀更多