在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
Microsoft Word 檔案,通常稱為 DOCX 檔案,是在各行各業中進行文件創建和交流的重要組成部分。 程序性生成和操作 Word 文件的能力對於使用 Visual Basic 的開發人員來說至關重要。
在本文中,我們將探討在 C# 應用程式中創建 Microsoft Word 文件的重要性,並介紹IronWord將該庫作為一個強大的工具來程式化地生成 Word 文件。
在 Visual Studio 中創建 Visual Basic 專案
從 NuGet 套件管理器安裝 IronWord 程式庫
建立一個TextRun和Paragraph
創建一個 WordDocument 類別對象
將 TextRun 和 Paragraph 添加到 WordDocument 對象中
IronWord是一個功能豐富的 C# DOCX 函式庫,由 IronSoftware 開發。 它使開發者能夠建構、編輯和匯出 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 套件」。
在套件管理器控制台中運行以下命令:
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 文件匯出為名為 "document.docx" 的 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 檔案的建立和操作。