VS 2022 程式化創建新 Word 文件(教程)
在數位時代,文件正日益數位化。 在商業環境中,文件通常與微軟 Word 及其文件型 Word 文件相關聯。撰寫和編輯微軟 Word文件已成為大型組織中訊息傳遞的基礎。 然而,手動建立和編輯文件可能是一個痛苦的過程; 以程式設計方式自動產生 Word 文件也不是一件容易的事,因為許多開源程式庫都依賴 Microsoft Office Word。
然而,管理和建立 Word 文件並不一定非得這麼難。 IronWord是一個 C# Word 函式庫,它不依賴 Microsoft Office Word,允許使用者自訂整個文檔,同時也允許以程式設計方式產生文件和建立文件範本。
在今天的教程中,我將簡單解釋如何使用 IronWord 程式化創建 Microsoft Word 文檔,並提供簡單範例。
IronWord:AC# 字庫
IronWord 是一款極其可靠且使用者友好的 C# Docx 程式庫,它使開發人員能夠使用 C# 建立和修改 Word 文檔,而無需依賴 Microsoft Office 或 Word Interop 等傳統程式庫。此外,它擁有豐富的文檔,並全面支援 .NET 8、7、6、.NET Framework、.NET Core 和 Azure,使其與大多數應用程式具有廣泛的兼容性。 這種靈活性使其成為您可能遇到的任何應用的絕佳選擇。
建立環境
在這個範例中,我們將使用Visual Studio建立一個控制台應用程式,並展示如何建立空白 Word 文件以及如何使用 IronWord 程式庫對其進行操作。 在進行下一步之前,請確保您已安裝 Visual Studio。
首先,我們建立一個新的控制台應用程式。
然後,提供項目名稱並儲存位置,如下所示。
最後,選擇所需的框架。
安裝 C# Word 庫
建立空白控制台應用程式後,我們將透過 NuGet 套件管理器下載IronWord 。
點擊"管理 NuGet 套件",然後在"瀏覽"標籤中搜尋 IronWord,如下所示。
之後,將其安裝到新建立的專案中。
或者,您也可以在命令列中輸入以下命令來安裝 IronWord。
Install-Package IronWord
現在一切都已準備就緒,讓我們來看一個建立 Word 文件的範例。
許可證密鑰
請注意,IronWord 需要許可證密鑰才能運作。 您可以點擊此連結以取得免費試用金鑰。
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
' Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"
收到試用金鑰後,請在項目中設定此變數。
建立新的 Word 文檔
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Create a textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Create a textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Create a textrun
Dim textRun As New Text("Sample text")
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
' Create a new Word document with the paragraph
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("document.docx")
End Sub
End Class
! VS 2022 以程式設計方式建立新 Word 文件(教學):圖 6 - 上述範例的輸出
說明
- 首先,我們在範例程式碼中導入 IronWord 函式庫及其模型。
- 我們建立一個新的
Text變數。 此變數允許我們在 Word 文件中新增文字。 - 然後我們將
textRun加到Paragraph。 這樣我們就可以透過區分兩者來輕鬆處理文字。 - 我們將
paragraph參數傳遞給新的WordDocument類,以建立新的文件物件。 - 最後,我們將文件儲存為 Word 文件"document.docx"。
在前面的範例中,我們建立了一個包含基本文字的 Word 文件。 讓我們來看一個使用更高級功能的例子,例如自訂文字和添加文字效果。
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY";
// Load an existing docx
WordDocument doc = new WordDocument("document.docx");
// Create sample texts with styles
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
// Create a paragraph and add texts
Paragraph paragraph = new Paragraph();
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph to the document
doc.AddParagraph(paragraph);
// Export the document
doc.SaveAs("save_document.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY";
// Load an existing docx
WordDocument doc = new WordDocument("document.docx");
// Create sample texts with styles
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
// Create a paragraph and add texts
Paragraph paragraph = new Paragraph();
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph to the document
doc.AddParagraph(paragraph);
// Export the document
doc.SaveAs("save_document.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY"
' Load an existing docx
Dim doc As New WordDocument("document.docx")
' Create sample texts with styles
Dim introText As New Text("This is an example paragraph with italic and bold styling.")
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New Text("Italic example sentence.")
italicText.Style = italicStyle
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New Text("Bold example sentence.")
boldText.Style = boldStyle
' Create a paragraph and add texts
Dim paragraph As New Paragraph()
paragraph.AddText(introText)
paragraph.AddText(italicText)
paragraph.AddText(boldText)
' Add paragraph to the document
doc.AddParagraph(paragraph)
' Export the document
doc.SaveAs("save_document.docx")
End Sub
End Class
程式碼解釋
- 與上面的範例一樣,我們建立
Text對象,並新增文件的範例文字。 - 然後我們建立一個新的
TextStyle對象,並將屬性IsItalic賦值為true,表示文字應為斜體。 - 我們對
boldText變數執行相同的操作,將true賦值給屬性IsBold。 - 然後我們將
TextStyle變數分配給它們各自的Text變數。 - 我們建立了
Paragraph變量,並呼叫AddText方法來新增斜體和粗體文字。 - 我們使用
AddParagraph方法將段落加入文件中。 - 最後,我們將文件儲存為"save_document.docx"。
以上範例展示了開發人員可以使用 IronWord 的字體和樣式。 除了斜體和粗體文字效果外,IronWord 還提供其他功能,確保開發人員在各種情況下都能建立獨特的 Word 文件。
結論
! VS 2022 以程式設計方式建立新 Word 文件(教學):圖 8 - IronWord 授權資訊
我們的示範表明,使用IronWord庫以 C# 程式設計方式建立 Word 文件是多麼容易。 該程式庫的靈活性和可擴展性使其成為開發人員在實際場景中(例如在 Word 文件中自訂文字、字體和樣式)的寶貴工具。 了解 Word 如何與其他應用程式集成,可以為開發人員提供更多應對挑戰的解決方案。
IronWord 提供免費試用授權。
常見問題解答
我如何在 C# 中程式化創建 Microsoft Word 文件?
您可以使用 IronWord 庫在 C# 中程式化創建 Microsoft Word 文件。初始化一個 WordDocument 對象,添加必要的內容,並使用 SaveAs 方法將其導出為 .docx 文件。
為什麼使用 IronWord 比使用 Microsoft Office Interop 創建 Word 文件更好?
IronWord 提供比 Microsoft Office Interop 更多的好處,因為它不需要安裝 Microsoft Office ,提高了靈活性,並且可以在不同平台上更容易集成而無需依賴 Office。
如何在 Visual Studio 中安裝 IronWord?
要在 Visual Studio 中安裝 IronWord,打開 NuGet 包管理器,搜尋 IronWord,並將其安裝到您的專案中。或者,利用命令行使用 Install-Package IronWord。
我可以使用 IronWord 格式化 Word 文件中的文本嗎?
是的,您可以使用 IronWord 格式化 Word 文檔中的文本,使用 TextStyle 對象將斜體和粗體等樣式應用到文本元素。
IronWord 與 .NET 和 Azure 相容嗎?
是的,IronWord 完全相容於 .NET 8, 7, 6, Framework, Core 和 Azure,使其能夠用於各種應用。
如何解決 IronWord 的安裝問題?
如果您遇到 IronWord 的安裝問題,請確保您使用的是兼容的 Visual Studio 和 .NET 版本,並且您的 NuGet 包管理器已更新。檢查網路連接並重試安裝過程。
開始使用 IronWord 創建 Word 文件需要什麼?
要開始使用 IronWord 創建 Word 文件,請將 IronWord 庫和其模型導入到您的 C# 專案中。然後,初始化一個 WordDocument 物件來開始構建您的文件。
如何使用 IronWord 程式化添加段落到 Word 文件中?
您可以使用 IronWord 程式化地將段落添加到 Word 文件中,方法是創建 Paragraph 對象,將文本添加到其中,並將其包含到 WordDocument 中。


