使用 IRONWORD 如何使用 C# 將 Word 文檔閱讀時保留格式 Jordi Bardia 更新:2026年1月18日 下載 IronWord NuGet 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 Microsoft Word 文件通常包含豐富的格式,例如字體、樣式和各種元素,使其在視覺上具有吸引力。 IronWord是Iron Software出品的一款功能強大的函式庫,它擁有直覺的 C# 和 VB .NET Word 和 Docx 文件 API。 無需安裝 Microsoft Office 或 Word Interop 即可建立、編輯和匯出 Word 文件。 IronWord完全支援.NET 8、7、6、Framework、Core 和 Azure。 這意味著該庫不需要在電腦上安裝 Word,並且可以獨立讀取檔案。 如果您正在使用 C# 並且需要讀取 Word 文件並保留其格式,本教學將指導您使用IronWord庫完成此過程。 如何在 C# 中讀取帶有格式的 Word 文檔 安裝IronWord庫以讀取 Word 文件。 使用IronWord庫中的 WordDocument 類別載入輸入 Word 文件"sample.docx"。 使用載入的 Word 文件閱讀帶有格式的段落。 在控制台輸出中顯示擷取的資料及其格式資訊。 先決條件 Visual Studio:確保您已安裝 Visual Studio 或任何其他 C# 開發環境。 NuGet套件管理器:確保您可以使用NuGet管理專案中的套件。 步驟 1:建立一個新的 C# 項目 建立一個新的 C# 控制台應用程序,或使用一個現有的專案來讀取 Word 文件。 選擇控制台應用程式模板,然後按一下下一步。 按一下"下一步"按鈕,提供解決方案名稱、專案名稱和程式碼路徑。 然後選擇所需的.NET版本。 最佳實務是始終選擇可用的最新版本,但如果您的專案有特定要求,請使用必要的.NET版本。 步驟 2:安裝IronWord函式庫 開啟您的 C# 項目,並使用NuGet套件管理器控制台安裝IronWord庫: Install-Package IronWord 也可以使用 Visual Studio 的NuGet套件管理器來安裝NuGet套件,如下圖所示。 步驟 3:閱讀帶有格式的 Word 文檔 要讀取 Word 文件,首先需要建立一個新文檔,然後在其中添加一些內容,如下所示。 現在將檔案儲存到專案目錄,並更改檔案屬性,將其複製到輸出目錄。 現在將以下程式碼片段加入到 program.cs 檔案中: using IronWord; class Program { static void Main() { try { // Load existing docx var sampleDoc = new WordDocument("sample.docx"); var paragraphs = sampleDoc.Paragraphs; // Iterate through each paragraph in the Word document foreach (var paragraph in paragraphs) { var textRun = paragraph.FirstTextRun; var text = textRun.Text; // Read text content // Extract Formatting details if available if (textRun.Style != null) { var fontSize = textRun.Style.FontSize; // Font size var isBold = textRun.Style.IsBold; Console.WriteLine($"\tText: {text}, FontSize: {fontSize}, Bold: {isBold}"); } else { // Print text without formatting details Console.WriteLine($"\tText: {text}"); } } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } using IronWord; class Program { static void Main() { try { // Load existing docx var sampleDoc = new WordDocument("sample.docx"); var paragraphs = sampleDoc.Paragraphs; // Iterate through each paragraph in the Word document foreach (var paragraph in paragraphs) { var textRun = paragraph.FirstTextRun; var text = textRun.Text; // Read text content // Extract Formatting details if available if (textRun.Style != null) { var fontSize = textRun.Style.FontSize; // Font size var isBold = textRun.Style.IsBold; Console.WriteLine($"\tText: {text}, FontSize: {fontSize}, Bold: {isBold}"); } else { // Print text without formatting details Console.WriteLine($"\tText: {text}"); } } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } $vbLabelText $csharpLabel 上面的程式碼使用IronWord庫類別 WordDocument 建構方法讀取 Word 文件。 輸出 解釋 1.開啟 Word 文件:使用IronWord中的 WordDocument 載入 Word 文件。 2.遍歷段落和段落:使用巢狀循環遍歷段落和段落。 樂段代表具有特定格式的文字部分。 3.提取文字和格式:從每次運行中提取文字內容並檢查格式屬性。 在這個例子中,我們示範如何提取字體大小和粗體格式。 4.處理異常:使用 try-and-catch 區塊來處理任何異常並列印它們。 載入的文件可用於列印文檔,我們也可以在樣式物件中變更字體顏色。 從 Word 文件中讀取表格 我們也可以讀取 Word 文件中的表格。 將以下程式碼片段加入程式中。 using IronWord; class Program { static void Main() { try { // Load existing docx var sampleDoc = new WordDocument("sample.docx"); // Read Tables var tables = sampleDoc.Tables; foreach (var table in tables) { var rows = table.Rows; foreach (var row in rows) { foreach (var cell in row.Cells) { var contents = cell.Contents; contents.ForEach(x => Console.WriteLine(x)); // Print cell contents } } } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } using IronWord; class Program { static void Main() { try { // Load existing docx var sampleDoc = new WordDocument("sample.docx"); // Read Tables var tables = sampleDoc.Tables; foreach (var table in tables) { var rows = table.Rows; foreach (var row in rows) { foreach (var cell in row.Cells) { var contents = cell.Contents; contents.ForEach(x => Console.WriteLine(x)); // Print cell contents } } } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } $vbLabelText $csharpLabel 這裡我們使用 Tables 類別的屬性來取得文件中的所有表格,然後遍歷它們並列印內容。 為現有文字新增樣式 我們可以使用IronWord庫為現有的 Word 文件添加新的樣式訊息,如下面的程式碼片段所示。 using IronWord; using IronWord.Models; class Program { static void Main() { try { // Load existing docx var sampleDoc = new WordDocument("sample.docx"); var paragraphs = sampleDoc.Paragraphs; // Iterate through paragraphs foreach (var paragraph in paragraphs) { var textRun = paragraph.FirstTextRun; var text = textRun.Text; // Read text content // Extract Formatting details if available if (textRun.Style != null) { var fontSize = textRun.Style.FontSize; // Font size var isBold = textRun.Style.IsBold; Console.WriteLine($"\tText: {text}, FontSize: {fontSize}, Bold: {isBold}"); } else { // Print text without formatting details Console.WriteLine($"\tText: {text}"); } } // Change the formatting of the text var style = new TextStyle() { FontFamily = "Caveat", FontSize = 72, TextColor = new IronColor(System.Drawing.Color.Blue), // Blue color IsBold = true, IsItalic = true, IsUnderline = true, IsSuperscript = false, IsStrikethrough = true, IsSubscript = false }; paragraphs[1].FirstTextRun.Style = style; // Save the document with the new style applied sampleDoc.SaveAs("sample2.docx"); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } using IronWord; using IronWord.Models; class Program { static void Main() { try { // Load existing docx var sampleDoc = new WordDocument("sample.docx"); var paragraphs = sampleDoc.Paragraphs; // Iterate through paragraphs foreach (var paragraph in paragraphs) { var textRun = paragraph.FirstTextRun; var text = textRun.Text; // Read text content // Extract Formatting details if available if (textRun.Style != null) { var fontSize = textRun.Style.FontSize; // Font size var isBold = textRun.Style.IsBold; Console.WriteLine($"\tText: {text}, FontSize: {fontSize}, Bold: {isBold}"); } else { // Print text without formatting details Console.WriteLine($"\tText: {text}"); } } // Change the formatting of the text var style = new TextStyle() { FontFamily = "Caveat", FontSize = 72, TextColor = new IronColor(System.Drawing.Color.Blue), // Blue color IsBold = true, IsItalic = true, IsUnderline = true, IsSuperscript = false, IsStrikethrough = true, IsSubscript = false }; paragraphs[1].FirstTextRun.Style = style; // Save the document with the new style applied sampleDoc.SaveAs("sample2.docx"); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } $vbLabelText $csharpLabel 這裡我們創建了 TextStyle 並將其添加到現有的段落物件中。 在 Word 文件中新增新的樣式內容 我們可以為已載入的 Word 文件中新增內容,如下面的程式碼片段所示。 using IronWord; using IronWord.Models; class Program { static void Main() { try { // Load Word Document var sampleDoc = new WordDocument("sample.docx"); var paragraphs = sampleDoc.Paragraphs; // Iterate through paragraphs foreach (var paragraph in paragraphs) { var textRun = paragraph.FirstTextRun; var text = textRun.Text; // Read text content // Extract the formatting details if available if (textRun.Style != null) { var fontSize = textRun.Style.FontSize; // Font size var isBold = textRun.Style.IsBold; Console.WriteLine($"\tText: {text}, FontSize: {fontSize}, Bold: {isBold}"); } else { // Print text without formatting details Console.WriteLine($"\tText: {text}"); } } // Add TextRun with Style to Paragraph TextRun blueTextRun = new TextRun(); blueTextRun.Text = "Add text using IronWord"; blueTextRun.Style = new TextStyle() { FontFamily = "Caveat", FontSize = 72, TextColor = new IronColor(System.Drawing.Color.Blue), // Blue color IsBold = true, IsItalic = true, IsUnderline = true, IsSuperscript = false, IsStrikethrough = true, IsSubscript = false }; paragraphs[1].AddTextRun(blueTextRun); // Add New Content to the Word file and save Paragraph newParagraph = new Paragraph(); TextRun newTextRun = new TextRun("New Add Information"); newParagraph.AddTextRun(newTextRun); // Configure the text with different styles TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); TextStyle italicStyle = new TextStyle() { IsItalic = true }; TextRun italicText = new TextRun("Italic example sentence.", italicStyle); TextStyle boldStyle = new TextStyle() { IsBold = true }; TextRun boldText = new TextRun("Bold example sentence.", boldStyle); // Add the styled text to the paragraph newParagraph.AddTextRun(introText); newParagraph.AddTextRun(italicText); newParagraph.AddTextRun(boldText); // Save the modified document sampleDoc.SaveAs("sample2.docx"); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } using IronWord; using IronWord.Models; class Program { static void Main() { try { // Load Word Document var sampleDoc = new WordDocument("sample.docx"); var paragraphs = sampleDoc.Paragraphs; // Iterate through paragraphs foreach (var paragraph in paragraphs) { var textRun = paragraph.FirstTextRun; var text = textRun.Text; // Read text content // Extract the formatting details if available if (textRun.Style != null) { var fontSize = textRun.Style.FontSize; // Font size var isBold = textRun.Style.IsBold; Console.WriteLine($"\tText: {text}, FontSize: {fontSize}, Bold: {isBold}"); } else { // Print text without formatting details Console.WriteLine($"\tText: {text}"); } } // Add TextRun with Style to Paragraph TextRun blueTextRun = new TextRun(); blueTextRun.Text = "Add text using IronWord"; blueTextRun.Style = new TextStyle() { FontFamily = "Caveat", FontSize = 72, TextColor = new IronColor(System.Drawing.Color.Blue), // Blue color IsBold = true, IsItalic = true, IsUnderline = true, IsSuperscript = false, IsStrikethrough = true, IsSubscript = false }; paragraphs[1].AddTextRun(blueTextRun); // Add New Content to the Word file and save Paragraph newParagraph = new Paragraph(); TextRun newTextRun = new TextRun("New Add Information"); newParagraph.AddTextRun(newTextRun); // Configure the text with different styles TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); TextStyle italicStyle = new TextStyle() { IsItalic = true }; TextRun italicText = new TextRun("Italic example sentence.", italicStyle); TextStyle boldStyle = new TextStyle() { IsBold = true }; TextRun boldText = new TextRun("Bold example sentence.", boldStyle); // Add the styled text to the paragraph newParagraph.AddTextRun(introText); newParagraph.AddTextRun(italicText); newParagraph.AddTextRun(boldText); // Save the modified document sampleDoc.SaveAs("sample2.docx"); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } $vbLabelText $csharpLabel 在這裡,我們創建了帶有樣式資訊的新 TextRun 和 Paragraph 對象,並將它們添加到已加載的 Word 文件中。 授權許可(提供免費試用) 取得您的IronWord免費試用授權金鑰。 此密鑰需要放置在 appsettings.json 中。 { "IronWord.LicenseKey": "IRONWORD.MYLICENSE.KEY.TRIAL" } 請提供您的電子郵件地址以取得試用許可證。 提交您的電子郵件地址後,金鑰將透過電子郵件發送給您。 結論 IronWord提供了一種便捷的方式,可以使用 C# 讀取帶有格式的 Word 文件。 根據您的具體需求和您正在處理的文件的複雜性,擴展提供的程式碼。 本教學課程旨在為將IronWord整合到您的 C# 應用程式中以進行 Word 文件處理提供一個起點。 常見問題解答 如何在 C# 中以格式閱讀 Word 文件? 要在 C# 中以格式閱讀 Word 文件,請使用 IronWord 庫。 首先通過 NuGet 包管理器安裝 IronWord。 使用 WordDocument 類加載文檔,並遍歷段落以提取文本和格式詳細信息。 設置 C# 項目以閱讀 Word 文件的步驟是什麼? 要設置用於閱讀 Word 文件的 C# 項目,請安裝 Visual Studio 或另一個 C# 開發環境。 使用 NuGet 包管理器將 IronWord 添加到您的項目中。 使用 WordDocument 類加載 Word 文檔以訪問其內容。 如何在 C# 中閱讀 Word 文件時處理異常? 使用 IronWord 在 C# 中閱讀 Word 文檔時,通過在文檔處理代碼周圍實施 try-catch 塊來處理異常。 這將有助於管理運行時錯誤並確保應用程序行為穩健。 我可以使用 C# 從 Word 文檔中讀取表格嗎? 是的,您可以使用 C#中的 IronWord 從 Word 文檔中讀取表格。 應通過 WordDocument 類的 Tables 屬性訪問表格並根據需要遍歷表格數據。 如何使用 C# 修改 Word 文檔中的文本樣式? 通過創建 TextStyle 對象並將其應用於特定文字運行或段落,使用 IronWord 修改 Word 文檔中的文本樣式。 這可以讓您自定義字體、大小和其他樣式屬性。 是否可以在 C# 中向 Word 文檔添加新內容? 是的,您可以使用 C# 中的 IronWord 向 Word 文檔添加新內容。 創建 TextRun 和 Paragraph 對象以向文檔添加樣式化內容,然後保存您的更改。 如何在 C# 中保存對 Word 文檔的修改? 在使用 IronWord 編輯 Word 文檔後,調用 WordDocument 實例的 Save 方法來保存更改。 指定文件路徑以創建應用修改的新文檔。 我需要安裝 Microsoft Office 才能在 C# 中處理 Word 文件嗎? 不,您不需要安裝 Microsoft Office 即可在 C# 中使用 IronWord 處理 Word 文檔。 該庫獨立於 Microsoft Office 工作,允許您直接處理 Word 文件。 哪些 .NET 版本與文字處理庫兼容? IronWord 與多個 .NET 版本兼容,包括 .NET 8、7、6、Framework、Core 和 Azure。 這確保它能滿足各種項目要求和環境。 如何獲取 C# 中文字處理庫的試用許可? 要獲取 IronWord 的試用許可,請訪問 Iron Software 網站並提供您的電子郵件地址。 您將通過電子郵件收到試用許可密鑰,您可以將其添加到 appsettings.json 文件中。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新2026年3月1日 如何使用 IronWord 在 C# 中建立可填寫的表格範本 學習如何使用 IronWord 在 C# 中建立可填寫的表單範本。 閱讀更多 更新2025年9月18日 ASP .NET Core 導入和導出 Word 文件 本指南探討如何使用 IronWord 庫導入現有的 Word 文件,顯示其內容,並從頭開始創建文件 閱讀更多 更新2025年10月11日 VS 2022 程式化創建新 Word 文件(教程) 在今天的教程中,我將簡單解釋如何使用 IronWord 程式化創建 Microsoft Word 文檔,並提供簡單範例。 閱讀更多 如何在 C# 中不使用 Office Interop 創建 Word 文檔3 個 C# Word 庫(為開發人員...
更新2025年10月11日 VS 2022 程式化創建新 Word 文件(教程) 在今天的教程中,我將簡單解釋如何使用 IronWord 程式化創建 Microsoft Word 文檔,並提供簡單範例。 閱讀更多