使用 IRONWORD 如何在 C# 中使用 Word 模板生成 Word 文檔 Jordi Bardia 更新:6月 22, 2025 下載 IronWord NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在現代應用中,即時產生 Word 文件以用於各種用途(例如帳單、發票、信件等)至關重要。 Microsoft Word 的範本文件功能提供了一種強大的方法來確保一致性和效率。 然而,手動填寫這些模板既費時又容易出錯。 這時, Iron Software的IronWord就派上了用場——這是一個強大的 .NET 函式庫,旨在以程式設計方式自動填入 Word 範本。 在本文中,我們將逐步介紹如何使用IronWord填入 Word 文件模板,並提供一個實際範例來說明流程。 如何在 C# 中使用 Word 範本產生 Word 文檔 在 Microsoft Visual Studio 中建立一個新專案。 透過 NuGet 套件管理器安裝IronWord 。 建立 Word 範本文件。 將資料插入 Word 文件並儲存為新文件。 為產生的 Word 文件新增文字效果。 IronWord是什麼? IronWord是Iron Software開發的一個 .NET 程式庫,旨在以程式設計方式簡化 Microsoft Word 文件的建立、操作和管理。 它允許開發人員自動產生 Word 文檔,從而更容易在應用程式中動態建立報告、發票、信函和其他類型的文檔。 IronWord 的主要特點 1. C# 填入 Word 範本和處理 IronWord允許使用 Word 範本在範本文件中定義佔位符,並在執行時將其替換為實際資料。 2. 文字處理 您可以輕鬆地在 Word 文件中插入、取代或刪除文字。 3. 格式設定 該庫支援多種格式設定選項,包括字體樣式、大小、顏色和段落對齊方式。 4. 表格和圖片 IronWord可讓您在文件中插入和操作表格和映像。 5. 相容性 它可與不同版本的 Microsoft Word 無縫協作,確保相容性和易用性。 使用案例 *報表產生*:利用動態資料自動產生詳細報表。 建立發票:填寫客戶和交易詳情,建立專業發票。 合約管理:自動建立包含個人化資訊的合約。 信函和通知**:為客戶或員工產生個人化的信函和通知。 IronWord 簡化了在 .NET 應用程式中處理 Word 文件的操作,對於希望自動化文件產生和管理任務的開發人員來說,它是一款非常有價值的工具。 先決條件 開始之前,請務必準備好以下物品: 您的電腦上已安裝 Visual Studio。 已安裝最新版本的 .NET Framework。 步驟 1:在 Microsoft Visual Studio 中建立一個新專案。 現在,讓我們從建立一個新的 Visual Studio 專案開始。 如何在 C# 中使用 Word 範本產生 Word 文件:圖 1 請在下方畫面上選擇控制台應用程式範本。 如何在 C# 中使用 Word 範本產生 Word 文件:圖 2 - 選擇控制台應用程式 提供專案名稱和地點。 如何在 C# 中使用 Word 範本產生 Word 文件:圖 3 - 提供名稱和位置 選擇 .NET 版本,最好選擇支援的最新版本,然後按一下"建立"。 如何在 C# 中使用 Word 範本產生 Word 文件:圖 4 步驟 2:安裝 IronWord NuGet 套件管理器。 在 Visual Studio 中,請依照下列步驟從 NuGet 套件管理器安裝 IronWord NuGet 套件。 如何在 C# 中使用 Word 範本產生 Word 文件:圖 5 - 從 NuGet 套件管理器中搜尋 IronWord 或者,請使用以下命令直接透過 CLI 安裝。 dotnet add package IronWord --version 2024.9.1 dotnet add package IronWord --version 2024.9.1 SHELL 步驟 3:建立 Word 範本文件。 現在,產生一個包含一到兩頁的 Word 範本文檔,以便在 Word 文檔產生過程中使用。 Dear {Name}, Thanks for purchasing {product}. We are happy to serve you always. Your application dated {Date} has been approved. The product comes with an expiry date of {expiryDate}. Renew the product on or before the expiry date. Feel free to contact {phone} or {email} for further queries. Address: {Address} Thank you, {Sender} 現在,將上述文件儲存為Template.docx 。 步驟 4:將資料插入 Word 文件並另存為新文件。 using System; using System.Collections.Generic; using IronWord; class Program { static void Main() { // Set the license key for IronWord License.LicenseKey = "your key"; // Define paths for the template and the output file string templatePath = "Template.docx"; string outputPath = "FilledDocument.docx"; // Create a new instance of the WordDocument class using the template path WordDocument doc = new WordDocument(templatePath); // Define a dictionary of placeholders and their replacements var replacements = new Dictionary<string, string> { { "{Name}", "John Doe" }, { "{Date}", DateTime.Now.ToString("MMMM d, yyyy") }, { "{Address}", "123 Iron Street, Iron Software" }, { "{product}", "IronWord" }, { "{Sender}", "IronSoftware" }, { "{phone}", "+123 456789" }, { "{email}", "sale@ironsoftware.com" }, { "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") }, }; // Replace placeholders in the document with actual data foreach (var replacement in replacements) { doc.Texts.ForEach(x => x.Replace(replacement.Key, replacement.Value)); } // Save the filled document doc.Save(outputPath); // Notify the user that the document has been saved successfully Console.WriteLine("Document filled and saved successfully."); } } using System; using System.Collections.Generic; using IronWord; class Program { static void Main() { // Set the license key for IronWord License.LicenseKey = "your key"; // Define paths for the template and the output file string templatePath = "Template.docx"; string outputPath = "FilledDocument.docx"; // Create a new instance of the WordDocument class using the template path WordDocument doc = new WordDocument(templatePath); // Define a dictionary of placeholders and their replacements var replacements = new Dictionary<string, string> { { "{Name}", "John Doe" }, { "{Date}", DateTime.Now.ToString("MMMM d, yyyy") }, { "{Address}", "123 Iron Street, Iron Software" }, { "{product}", "IronWord" }, { "{Sender}", "IronSoftware" }, { "{phone}", "+123 456789" }, { "{email}", "sale@ironsoftware.com" }, { "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") }, }; // Replace placeholders in the document with actual data foreach (var replacement in replacements) { doc.Texts.ForEach(x => x.Replace(replacement.Key, replacement.Value)); } // Save the filled document doc.Save(outputPath); // Notify the user that the document has been saved successfully Console.WriteLine("Document filled and saved successfully."); } } Imports System Imports System.Collections.Generic Imports IronWord Friend Class Program Shared Sub Main() ' Set the license key for IronWord License.LicenseKey = "your key" ' Define paths for the template and the output file Dim templatePath As String = "Template.docx" Dim outputPath As String = "FilledDocument.docx" ' Create a new instance of the WordDocument class using the template path Dim doc As New WordDocument(templatePath) ' Define a dictionary of placeholders and their replacements Dim replacements = New Dictionary(Of String, String) From { {"{Name}", "John Doe"}, {"{Date}", DateTime.Now.ToString("MMMM d, yyyy")}, {"{Address}", "123 Iron Street, Iron Software"}, {"{product}", "IronWord"}, {"{Sender}", "IronSoftware"}, {"{phone}", "+123 456789"}, {"{email}", "sale@ironsoftware.com"}, {"{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy")} } ' Replace placeholders in the document with actual data For Each replacement In replacements doc.Texts.ForEach(Function(x) x.Replace(replacement.Key, replacement.Value)) Next replacement ' Save the filled document doc.Save(outputPath) ' Notify the user that the document has been saved successfully Console.WriteLine("Document filled and saved successfully.") End Sub End Class $vbLabelText $csharpLabel 說明 提供的程式碼示範如何使用 IronWord 函式庫將特定資料填入 Word 文件範本中。 以下是簡要說明: 1.許可證設定:代碼首先設定 IronWord 的許可證金鑰以啟動其功能。 2.檔案路徑:它指定 Word 範本 ( Template.docx ) 和輸出檔案 ( FilledDocument.docx ) 的路徑。 3.建立文件實例:使用範本路徑引用建立WordDocument實例。 4.定義替換:建立字典,其中鍵表示模板中的佔位符,值表示要插入的資料。 5.替換佔位符:它遍歷字典,將文件中的每個佔位符替換為對應的資料。 6.儲存文件:最後,將更新後的文件儲存到指定的輸出路徑。 7.完成資訊:列印一則資訊以確認文件已成功填寫並儲存。 Output 如何在 C# 中使用 Word 範本產生 Word 文件:圖 7 - Word 文件輸出 步驟 5:為產生的 Word 文件新增文字效果。 IronWord 還允許添加各種文字效果,如下表所示。 在以下範例中,我們為單字"IronSoftware"新增文字效果。 using System; using System.Collections.Generic; using IronWord; using IronWord.Models; class Program { static void Main() { // Set the license key for IronWord License.LicenseKey = "your key"; // Define paths for the template and the output file string templatePath = "Template.docx"; string outputPath = "glowEffect.docx"; // Create a new instance of the WordDocument class WordDocument doc = new WordDocument(templatePath); // Define a dictionary of placeholders and their replacements var replacements = new Dictionary<string, string> { { "{Name}", "John Doe" }, { "{Date}", DateTime.Now.ToString("MMMM d, yyyy") }, { "{Address}", "123 Iron Street, Iron Software" }, { "{product}", "IronWord" }, { "{Sender}", "Sale," }, { "{phone}", "+123 456789" }, { "{email}", "sale@ironsoftware.com" }, { "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") }, }; // Replace placeholders in the document with actual data foreach (var replacement in replacements) { doc.Texts.ForEach(x => x.Replace(replacement.Key, replacement.Value)); } // Create and configure text style methods with a glow effect TextStyle textStyle = new TextStyle { TextEffect = new TextEffect() { GlowEffect = new Glow() { GlowColor = IronWord.Models.Color.Aqua, GlowRadius = 10, }, } }; // Add styled text to the document doc.AddText(" IronSoftware").Style = textStyle; // Save the document with the glow effect doc.SaveAs(outputPath); // Notify the user that the document has been saved successfully Console.WriteLine("Styled document saved successfully."); } } using System; using System.Collections.Generic; using IronWord; using IronWord.Models; class Program { static void Main() { // Set the license key for IronWord License.LicenseKey = "your key"; // Define paths for the template and the output file string templatePath = "Template.docx"; string outputPath = "glowEffect.docx"; // Create a new instance of the WordDocument class WordDocument doc = new WordDocument(templatePath); // Define a dictionary of placeholders and their replacements var replacements = new Dictionary<string, string> { { "{Name}", "John Doe" }, { "{Date}", DateTime.Now.ToString("MMMM d, yyyy") }, { "{Address}", "123 Iron Street, Iron Software" }, { "{product}", "IronWord" }, { "{Sender}", "Sale," }, { "{phone}", "+123 456789" }, { "{email}", "sale@ironsoftware.com" }, { "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") }, }; // Replace placeholders in the document with actual data foreach (var replacement in replacements) { doc.Texts.ForEach(x => x.Replace(replacement.Key, replacement.Value)); } // Create and configure text style methods with a glow effect TextStyle textStyle = new TextStyle { TextEffect = new TextEffect() { GlowEffect = new Glow() { GlowColor = IronWord.Models.Color.Aqua, GlowRadius = 10, }, } }; // Add styled text to the document doc.AddText(" IronSoftware").Style = textStyle; // Save the document with the glow effect doc.SaveAs(outputPath); // Notify the user that the document has been saved successfully Console.WriteLine("Styled document saved successfully."); } } Imports System Imports System.Collections.Generic Imports IronWord Imports IronWord.Models Friend Class Program Shared Sub Main() ' Set the license key for IronWord License.LicenseKey = "your key" ' Define paths for the template and the output file Dim templatePath As String = "Template.docx" Dim outputPath As String = "glowEffect.docx" ' Create a new instance of the WordDocument class Dim doc As New WordDocument(templatePath) ' Define a dictionary of placeholders and their replacements Dim replacements = New Dictionary(Of String, String) From { {"{Name}", "John Doe"}, {"{Date}", DateTime.Now.ToString("MMMM d, yyyy")}, {"{Address}", "123 Iron Street, Iron Software"}, {"{product}", "IronWord"}, {"{Sender}", "Sale,"}, {"{phone}", "+123 456789"}, {"{email}", "sale@ironsoftware.com"}, {"{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy")} } ' Replace placeholders in the document with actual data For Each replacement In replacements doc.Texts.ForEach(Function(x) x.Replace(replacement.Key, replacement.Value)) Next replacement ' Create and configure text style methods with a glow effect Dim textStyle As New TextStyle With { .TextEffect = New TextEffect() With { .GlowEffect = New Glow() With { .GlowColor = IronWord.Models.Color.Aqua, .GlowRadius = 10 } } } ' Add styled text to the document doc.AddText(" IronSoftware").Style = textStyle ' Save the document with the glow effect doc.SaveAs(outputPath) ' Notify the user that the document has been saved successfully Console.WriteLine("Styled document saved successfully.") End Sub End Class $vbLabelText $csharpLabel 說明 修改後的程式碼示範如何使用 IronWord 函式庫來填寫 Word 文件範本、設定文字樣式並儲存修改後的文件。 以下是簡要說明: 1.許可證設定:設定 IronWord 許可證密鑰以啟用功能。 2.檔案路徑:指定範本( Template.docx )和輸出檔案( glowEffect.docx )的路徑。 3.建立文件實例:使用提供的範本路徑初始化WordDocument實例。 4.定義替換項:建立一個佔位符及其對應替換值的字典。 5.取代佔位符:遍歷字典,將文件中的佔位符替換為實際資料。 6.配置文字樣式:定義帶有發光效果的文字樣式,指定顏色和半徑。 7.新增樣式文字:向文件中新增具有已配置樣式的文字。 8.儲存文件:將更新後的文件儲存為新名稱( glowEffect.docx ),以反映套用的文字樣式。 9.控制台輸出:列印一則訊息以確認樣式文件已儲存。 Output 如何在 C# 中使用 Word 範本產生 Word 文件:圖 8 - Word 輸出範例 IronWord 授權 IronWord. 資料錄入完畢後,許可證將發送到所提供的電子郵件地址。 在使用IronWord函式庫之前,需要將此授權放在程式碼的開頭,如下所示。 License.LicenseKey = "your Key Here"; License.LicenseKey = "your Key Here"; License.LicenseKey = "your Key Here" $vbLabelText $csharpLabel 結論 IronWord在使用範本產生 Word 文件方面具有諸多優勢。 它簡化了文件創建自動化流程,允許開發人員以程式設計方式使用特定資料填充模板,從而減少了手動輸入的需要。 這樣可以提高效率和準確性,因為人為錯誤的風險降到了最低。 此外, IronWord還有助於保持文件之間的一致性,確保產生的每個文件都遵循相同的格式和結構。 自動化重複性任務可以節省時間和資源,因此非常適合快速產生大量文件。 IronWord可提高工作效率,並簡化需要頻繁或複雜文件產生的場景下的工作流程。 透過依照本文概述的步驟並利用提供的IronWord範例,您可以有效地管理文件產生需求並簡化工作流程。 常見問題解答 如何使用 C# 填寫 Word 文件範本? 您可以利用 IronWord,使用 C# 來填充 Word 文件範本。首先,在 Visual Studio 中設定專案,並透過 NuGet 安裝 IronWord 套件。建立 Word 模版並使用 IronWord 插入資料,然後將填滿的模版儲存為新的文件。 使用 .NET 函式庫進行 Word 模版自動化有什麼好處? 使用 IronWord 之類的 .NET 函式庫進行 Word 模版自動化,可以減少手動輸入、將錯誤減至最低,並確保文件建立的一致性。它可以有效率地處理帳單、發票和寫信等工作。 在以程式方式填寫 Word 模版時,可以加入文字效果嗎? 是的,使用 IronWord,您可以在以程式化方式填寫範本時,為 Word 文件中的文字加入光暈或陰影等文字效果。 在 Visual Studio 專案中設定 IronWord 涉及哪些步驟? 要在 Visual Studio 專案中設定 IronWord,首先要安裝 IronWord NuGet 套件,建立您的 Word 模版,然後使用 IronWord 的方法以程式化的方式填入並儲存文件。 IronWord 如何確保文件生成的一致性? IronWord 可讓開發人員使用 Word 範本,在多個文件中維持相同的格式和排版,減少人為錯誤的風險,從而確保一致性。 自動化 Word 文件產生有哪些實際應用? 使用 IronWord 自動化 Word 文件生成可應用於各種場景,包括報表生成、發票建立、合約管理以及製作個性化信件。 是否可以使用 IronWord 處理不同版本的 Microsoft Word? 是的,IronWord 與各種版本的 Microsoft Word 相容,可在不同的環境中無縫處理文件。 開始使用 IronWord 進行 Word 文件管理需要哪些條件? 要開始使用 IronWord,請確保您已安裝 Visual Studio 以及最新的 .NET Framework。然後,透過 NuGet 套件管理員將 IronWord 加入您的專案。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新9月 18, 2025 ASP .NET Core 匯入和匯出 Word 檔案 本指南探討如何匯入現有的 Word 文件、顯示其內容,以及使用 IronWord 函式庫從頭建立文件 閱讀更多 更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多 更新6月 22, 2025 如何使用 C# 在 Word 中對齊文字 讓我們深入瞭解 IronWord NuGet 套件,以及如何使用此套件對齊文字或段落 閱讀更多 如何使用 C# 在 Word 中對齊文字如何使用 C# 在 Word 檔案中...
更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多