如何使用 C# 取代 Word 文件中的文字
IronWord 可讓 C# 開發人員在不需要 Microsoft Office Interop 的情況下,以程式化的方式尋找和取代 Word 文件中的文字,提供自動化文件更新、個人化範本和大量內容管理的解決方案。
無論是個人化範本、更新報告或大量管理內容,在 Word 文件中自動取代文字都是常見的需求。IronWord 是一套 C# 程式庫,可簡化這項工作,從基本的尋找與取代到複雜的文件處理流程都能處理。
IronWord 可直接操作 Word 文件,而無需依賴 Microsoft Office Interop。這表示您不需要在伺服器上安裝 Word,進而為後端程序和 Web 應用程式提供更快速、可靠且可擴充的解決方案。 這種獨立於 Microsoft Office 的特性,讓 IronWord 對雲端部署、容器化應用程式和自動化文件處理管道特別有價值。
本指南透過完整的程式碼範例,示範如何在任何 Word 文件中尋找及取代文字。無論是單純取代文字,還是處理包含多個段落和不同格式的複雜文件,IronWord 都提供所需的工具。
快速入門:輕鬆取代 Word 文件中的文字本指南示範如何使用 IronWord 取代 Word 文件中的文字。 只需幾行 C# 程式碼,即可載入 DOCX 文件,取代指定文字,並儲存更新後的文件。 此流程無需 Microsoft Office Interop 即可運作,因此非常適合後端流程和 Web 應用程式。
-
1使用 IronWord 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
WordDocument doc = new WordDocument("sample.docx"); doc.ReplaceText("oldText", "newText"); doc.SaveAs("updated.docx");Dim doc As New WordDocument("sample.docx") doc.ReplaceText("oldText", "newText") doc.SaveAs("updated.docx")Dim doc As New WordDocument("sample.docx") doc.ReplaceText("oldText", "newText") doc.SaveAs("updated.docx") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronWord,並使用 免費試用
最簡工作流程(5 個步驟)
- 下載可取代 Word 文件中單字的 C# 程式庫
- 載入現有 Word 文件
- 存取 Word 文件的段落和文字
- 使用
ReplaceText將舊文字取代為新文字 - 儲存更新後的 Word 文件
如何在 Word 文件中取代文字?
使用 IronWord 取代 Word 文件中的文字時,先用 WordDocument 建構函式載入現有文件,再存取 Paragraphs 屬性並對指定段落呼叫 ReplaceText 方法。這樣就能精確控制要修改文件的哪些部分。
ReplaceText 方法接受兩個字串參數:要尋找的文字,以及用來取代它的新文字。此方法會取代指定段落中所有相符的文字,適合用來更新產品名稱、變更日期或個人化客戶資訊。
ReplaceText 方法區分大小寫,並取代所選段落中所有相符的字串。使用 Paragraphs 集合時,請留意 IronWord 如何組織文件內容。Word 文件中的每個段落都是集合中的獨立元素,因此您可以精確指定要處理的段落。
輸入文件的樣子如何?
本範例使用一份有兩個段落的Word 範例文件,兩段都包含「old text」。這個簡單的結構可用來說明如何取代指定段落中的文字。

如何撰寫取代程式碼?
以下程式碼會將第一段的「old text」取代為「new text」。透過索引指定段落,即可精確控制要修改的內容:
using IronWord;
// Open existing Word
WordDocument doc = new WordDocument("sample.docx");
// Replace the first paragraph's old text with new text
doc.Paragraphs[0].ReplaceText("old text", "new text");
// Save updated Word Document
doc.SaveAs("updated.docx");using IronWord;
// Open existing Word
WordDocument doc = new WordDocument("sample.docx");
// Replace the first paragraph's old text with new text
doc.Paragraphs[0].ReplaceText("old text", "new text");
// Save updated Word Document
doc.SaveAs("updated.docx");Imports IronWord
' Open existing Word
Dim doc As New WordDocument("sample.docx")
' Replace the first paragraph's old text with new text
doc.Paragraphs(0).ReplaceText("old text", "new text")
' Save updated Word Document
doc.SaveAs("updated.docx")Imports IronWord
' Open existing Word
Dim doc As New WordDocument("sample.docx")
' Replace the first paragraph's old text with new text
doc.Paragraphs(0).ReplaceText("old text", "new text")
' Save updated Word Document
doc.SaveAs("updated.docx")上述程式碼只需幾行即可載入文件、指定段落並呼叫 ReplaceText,從而修改文件內容。這比使用傳統的 Office Interop 方法更直接。
輸出看起來是怎樣的?

如輸出文件所示,只有第一段的文字變更,第二段維持原樣。若只需更新特定部分,這種做法可避免影響整份文件。
如何取代多次出現的文字?
若要取代整份文件中所有相符的文字,請逐一巡覽 Paragraphs 集合,並對每個段落呼叫 ReplaceText 方法。這樣無論目標文字出現在哪個段落,都不會遺漏。
沿用先前的範例檔案 sample.docx。如果某個段落找不到要取代的文字,迴圈便直接處理下一段。因此,取代前無須先檢查文字是否存在。
多次取代需要什麼程式碼?
以下程式碼會將整份文件中所有的「old text」取代為「new text」:
using IronWord;
// Open existing Word
WordDocument doc = new WordDocument("sample.docx");
// Loop through each paragraph to find and replace text
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
// Replace all occurrences of "old text" with "new text" in the current paragraph
doc.Paragraphs[i].ReplaceText("old text", "new text");
}
// Save updated Word Document
doc.SaveAs("updated.docx");using IronWord;
// Open existing Word
WordDocument doc = new WordDocument("sample.docx");
// Loop through each paragraph to find and replace text
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
// Replace all occurrences of "old text" with "new text" in the current paragraph
doc.Paragraphs[i].ReplaceText("old text", "new text");
}
// Save updated Word Document
doc.SaveAs("updated.docx");Imports IronWord
' Open existing Word
Dim doc As New WordDocument("sample.docx")
' Loop through each paragraph to find and replace text
For i As Integer = 0 To doc.Paragraphs.Count - 1
' Replace all occurrences of "old text" with "new text" in the current paragraph
doc.Paragraphs(i).ReplaceText("old text", "new text")
Next
' Save updated Word Document
doc.SaveAs("updated.docx")Imports IronWord
' Open existing Word
Dim doc As New WordDocument("sample.docx")
' Loop through each paragraph to find and replace text
For i As Integer = 0 To doc.Paragraphs.Count - 1
' Replace all occurrences of "old text" with "new text" in the current paragraph
doc.Paragraphs(i).ReplaceText("old text", "new text")
Next
' Save updated Word Document
doc.SaveAs("updated.docx")逐段巡覽可確保整份文件中所有相符的文字都被取代。這種做法特別適合大量更新,例如更改公司名稱、更新術語或統一整份文件的格式。
多次取代後文件會顯示什麼樣子?

如更新後的 Word 文件所示,兩個段落中的文字都已取代為「new text」,說明迴圈可逐段完成取代。
如何在取代前確認文字是否存在?
若要在操作前確認文字是否存在,請使用 FindText 方法。它會搜尋整份文件,並傳回第一個符合搜尋條件的 TextContent。需要先確認特定內容,或依文件內容撰寫條件邏輯時,可使用這項檢查。
FindText方法在以下情境中特別有價值:
- 您需要記錄哪些文件包含特定文字
- 您希望根據是否找到文字來執行不同的動作
- 您正在建立一個文件驗證系統
- 您需要就文件內容產生報告
以下舉例說明如何在上面提到的 Word 文件中搜尋舊文字。
如何用程式碼搜尋文字?
using IronWord;
using System;
// Open existing Word
WordDocument doc = new WordDocument("sample.docx");
// Returns the first TextContent element that contains the specified text.
Console.WriteLine(doc.FindText("old text"));using IronWord;
using System;
// Open existing Word
WordDocument doc = new WordDocument("sample.docx");
// Returns the first TextContent element that contains the specified text.
Console.WriteLine(doc.FindText("old text"));Imports IronWord
Imports System
' Open existing Word
Dim doc As New WordDocument("sample.docx")
' Returns the first TextContent element that contains the specified text.
Console.WriteLine(doc.FindText("old text"))Imports IronWord
Imports System
' Open existing Word
Dim doc As New WordDocument("sample.docx")
' Returns the first TextContent element that contains the specified text.
Console.WriteLine(doc.FindText("old text"))使用 IronWord 搜尋文字時,FindText 找到相符文字便會傳回 TextContent 物件,找不到則傳回 null。您可以根據傳回結果決定後續要執行的邏輯。
搜尋文字後會顯示什麼結果?

搜尋結果傳回了相符文字,確認文件中有要尋找的內容。處理不同內容的文件時,可用這項檢查來提升應用程式的可靠性。
FindText 沒有傳回結果,表示文件中沒有這段文字。請確認搜尋文字的拼寫正確,且與文件中的大小寫相符。若要從字典、DataTable 或 DataSet 等資料來源填入 MERGEFIELD 預留位置,而非取代字面字串,請參閱在 C# 中對 Word 文件執行合併列印的方法。
文字取代最佳實務
在 Word 文件中使用文字取代時,請考慮這些最佳實務:
- 區分大小寫:
ReplaceText區分大小寫。若要忽略大小寫,請先統一文字的大小寫。 - 效能最佳化: 批次取代後只儲存一次,以減少 I/O 作業並提升效能。
- 錯誤處理: 實作錯誤處理,以因應文件鎖定、損毀或內容不符預期的情況。
- 備份策略: 大量取代前請先備份,尤其是在正式環境中。
- 記憶體管理: 處理多份文件時,請妥善釋放文件物件,以避免記憶體洩漏。
IronWord 的 API 可簡化這些最佳實務的實作,協助您建立可靠的文件處理方案。
常見問題
如何使用 C# 在 Word 文件中替換文字?
您可以使用 IronWord 在 Word 文件中替換文字,方法是載入文件,利用 ReplaceText 方法指定舊文字和新文字,然後儲存文件。這省去了使用 Microsoft Office Interop 的需要。
使用 IronWord 替換文字的好處有哪些?
IronWord 獨立於 Microsoft Office,快速處理,可擴展性,並與雲端及容器化應用程式相容,使其成為自動化文件處理的理想選擇。
IronWord 如何處理文件中的多個文字替換?
要在 Word 文件中替換多次出現的文字,您可以循環遍歷每個段落,使用 Paragraphs 集合並套用 ReplaceText 方法以進行全面的文字替換。
IronWord 中的 ReplaceText 方法是什麼?
ReplaceText 方法是 IronWord 中的一個功能,允許開發人員在 Word 文件中查找並替換指定的文字。它具有多功能性,且可以套用於特定段落或整個文件。
IronWord 的文字替換功能是否區分大小寫?
是的,IronWord 的 ReplaceText 方法區分大小寫,因此需要準確匹配文本才能進行替換。您可能需要調整文字大小寫以獲得一致的結果。
使用IronWord需要安裝Microsoft Office嗎?
不,您不需要安裝 Microsoft Office 即可使用 IronWord。IronWord 獨立運作,允許不依賴 Office Interop 即可操作 Word 文件。
IronWord 可以在替換前找到文件中的文字嗎?
是的,IronWord 可以使用 FindText 方法搜尋文字,若找到會返回 TextContent 物件,有助於在文字替換前進行條件邏輯。
IronWord 提供哪些偵錯功能以便於文字替換?
IronWord 提供如 FindText 的方法來定位文字,這幫助確認替換前文字的存在,對於除錯文件處理邏輯至關重要。
我如何確保 IronWord 替換文件中所有目標文字?
透過使用循環遍歷每個段落並套用 ReplaceText 方法,您可以確保 IronWord 在文件中全面替換所有目標文字實例。
使用 IronWord 替換文字時應遵循哪些最佳實踐?
最佳實踐包括確保區分大小寫,透過分批替換來優化效能,實施錯誤處理,創建備份,以及有效管理記憶體。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。