在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
Microsoft Word 檔案,通常稱為 DOCX 檔案,是在各行各業中進行文件創建和交流的重要組成部分。 程序性生成和操作 Word 文件的能力對於使用 Visual Basic 的開發人員來說至關重要。
在本文中,我們將探討在 C# 應用程式中創建 Microsoft Word 文件的重要性,並介紹 IronWord 庫作為一個強大的工具,用於以程式化方式生成 Word 文件。
在 Visual Studio 中創建 Visual Basic 專案
從NuGet套件管理器安裝IronWord庫
創建TextRun 和 Paragraph
創建一個WordDocument類別物件
將TextRun和Paragraph新增到WordDocument物件
IronWord 是由 IronSoftware 開發的一個功能豐富的 C# DOCX 庫。 它使開發者能夠建構、編輯和匯出 Word 文件,而不需要 Microsoft Office 或 Word Interop。IronWord 支援多個 .NET 版本,為在各種專案類型和環境中處理 Word 文件提供了靈活解決方案。
主要功能:
IronWord 提供了全面的功能集,是用於在 C# 中操控 Word 文件的首選庫。 一些主要功能包括:
跨平台支持:IronWord 支援 C#、F# 和 VB.NET,可在各種 .NET 版本上運行,包括 8、7、6、5、Core、Standard 或 Framework。 它還適用於網路、行動裝置、桌面和主控台的應用程式。
文件結構操作:IronWord 允許開發人員讀取和編輯 Word 文件,提取文本和圖像,自定義頁面設置,並添加段落、文本運行、圖像、形狀和表格。
樣式和格式化: 該庫支持樣式選項,例如字體系列和大小、顏色、粗體、斜體、下劃線、上標、下標、對齊、背景顏色和項目符號/編號列表。
跨平台相容性:IronWord 無縫運作於不同的作業系統,包括 Windows、Linux、macOS、iOS 和 Android。
打開 Visual Studio。
創建一個新的 C# Console .NET 應用程式。
從「其他資訊」中選擇最新版本的 .NET Framework。 IronWord 支援最新的 8.0 .NET Core 框架。
要將IronWord整合到您的專案中以建立一個簡單的Word文件,請按照以下步驟操作:
在方案總管中右鍵點擊您的項目。
選擇「管理方案的 NuGet 套件」。
如何在 VB .NET 中以程式方式建立 Word 文件:圖 3 - 在 NuGet 套件管理器中安裝 IronWord
在套件管理器控制台中運行以下命令:
Install-Package IronWord
讓我們通過一個簡單的例子來展示如何在 VB.NET 中使用 IronWord 創建一個 Word 文檔:
Imports IronWord
Imports IronWord.Models
Module Program
Sub Main()
' Create a textrun
Dim textRun As New TextRun("Sample text")
' Create a paragraph and add the textrun
Dim paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a new Word document using the paragraph
Dim document As New WordDocument(paragraph)
' Export the document as a docx file
document.SaveAs("document.docx")
End Sub
End Module
Imports IronWord
Imports IronWord.Models
Module Program
Sub Main()
' Create a textrun
Dim textRun As New TextRun("Sample text")
' Create a paragraph and add the textrun
Dim paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a new Word document using the paragraph
Dim document As New WordDocument(paragraph)
' Export the document as a docx file
document.SaveAs("document.docx")
End Sub
End Module
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Imports IronWord @Imports IronWord.Models Module Program @Sub Main() ' Create a textrun @Dim textRun @As @New TextRun("Sample text") ' Create a paragraph @and add the textrun @Dim paragraph @As @New Paragraph() paragraph.AddTextRun(textRun) ' Create a New Word document using the paragraph @Dim document @As @New WordDocument(paragraph) ' Export the TryCast(document, a) docx file document.SaveAs("document.docx") @End @Sub @End Module
代碼講解
讓我們分解這段 VB.NET 代碼中 創建 Word 文件 的關鍵組成部分:
導入IronWord命名空間:
Imports IronWord
Imports IronWord.Models
Imports IronWord
Imports IronWord.Models
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Imports IronWord @Imports IronWord.Models
這些行從 IronWord 函式庫中匯入必要的命名空間,以便在您的 VB.NET 代碼中使用類和 Word 文件模型。
建立 TextRun 和 Paragraph:
Dim textRun As New TextRun("Sample text")
Dim paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
Dim textRun As New TextRun("Sample text")
Dim paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Dim textRun @As @New TextRun("Sample text") @Dim paragraph @As @New Paragraph() paragraph.AddTextRun(textRun)
在這裡,創建了一個包含內容「Sample text」的TextRun,並創建了一個Paragraph來容納TextRun。 這將以 Word 文件的內容結構設置模板文件。
創建 Word 文件:
以下程式碼建立一個新的文件對象並向其中添加一個文字段落:
Dim document As New WordDocument(paragraph)
Dim document As New WordDocument(paragraph)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Dim document @As @New WordDocument(paragraph)
創建了一個WordDocument類的實例,並將Paragraph傳遞給其構造函數。 這會以指定的內容初始化 Word 文件。
匯出文件:
document.SaveAs("document.docx")
document.SaveAs("document.docx")
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'document.SaveAs("document.docx")
最後,SaveAs 方法用於將 Word 文件作為 DOCX 文件匯出,文件名為「document.docx」。
輸出:
執行程式後,一個名為 "output.docx" 的 Word 文件將在專案目錄中生成。 開啟此文件以查看新增內容。
IronWord 提供簡便性,並允許編輯和操作特定段落。若要獲取更多關於文件類方法的信息,請訪問此程式碼範例頁面。
在本文中,我們探討了在 C# 應用程式中創建 Microsoft Word 文件的重要性,並介紹了IronWord庫作為完成此任務的強大工具。 IronWord 擁有豐富的功能集、跨平台支持以及易於整合到 Visual Studio 專案中,簡化了以程式方式處理 Word 文件的過程。 開發人員可以利用 IronWord 來增強文件處理功能,並簡化其 C# 應用程式中 Word 檔案的建立和操作。
如需有關 IronWord VB.NET 及其功能的詳細信息,請訪問 IronWord 官方網站上的文檔頁面。
IronWord 可免費用於開發目的,並可以授權來在商業模式下測試其完整功能後再做決定。 從這裡下載這個庫並試用一下。
在本文中,我們探討了在 C# 應用程式中建立 Microsoft Word 文件的重要性,並介紹了 IronWord 程式庫作為實現這一任務的強大工具。 IronWord 擁有豐富的功能集、跨平台支持以及易於整合到 Visual Studio 專案中,簡化了以程式方式處理 Word 文件的過程。 開發人員可以利用 IronWord 來增強文件處理功能,並簡化其 C# 應用程式中 Word 檔案的建立和操作。