在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
在数字时代,文件越来越数字化。 商业环境中的文档通常与 Microsoft Word 及其基于文档的 Word 文件有关。写作和编辑Microsoft Word文档已成为大型组织传递信息的基础。 然而,手动创建和编辑文档是一个痛苦的过程; 要以编程方式自动生成 Word 也并非易事,因为许多开源库都依赖于 Microsoft Office Word。
然而,管理和创建 Word 文档并不需要如此困难。 IronWord是一个不依赖于 Microsoft Office Word 的 C# Word 库,允许用户自定义整个文档,同时还允许以编程方式生成文档和创建文档模板。
在今天的教程中,我将简要介绍如何使用 IronWord 以编程方式创建 Microsoft Word 文档,并提供简要示例。
破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。
IronWord 是一个异常可靠且用户友好的 C# Docx 库,它使开发人员能够使用 C# 构建和修改 Word 文档,而无需 Microsoft Office 或 Word Interlope 等传统依赖项。 此外,它还拥有丰富的文档,并提供对 .NET 8、7、6、Framework、Core 和 Azure 的全面支持,使其与大多数应用程序普遍兼容。 这种灵活性使其成为您处理任何应用程序的绝佳选择。
在本示例中,我们将使用Visual Studio并展示如何使用 IronWord 库创建空白 Word 文档并对其进行操作。 在进行下一步之前,请确保您已安装 Visual Studio。
首先,让我们创建一个新的控制台应用程序。
破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。
然后,提供一个项目名称并保存位置,如下所示。
破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。
最后,选择所需的框架。
破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。
创建空白控制台应用程序后,我们将下载IronWord通过 NuGet 软件包管理器。
点击 "管理 NuGet 包",然后在 "浏览选项卡 "中搜索 IronWord,如下图所示。
破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。
然后,将其安装到新创建的项目中。
或者,您也可以在命令行中键入以下命令来安装 IronWord。
Install-Package IronWord
Install-Package IronWord
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'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";
IRON VB CONVERTER ERROR developers@ironsoftware.com
收到试用密钥后,请在项目中设置此变量。
using IronWord;
using IronWord.Models;
// Create textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
using IronWord;
using IronWord.Models;
// Create textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models
' Create textrun
Private textRun As New Text("Sample text")
Private paragraph As New Paragraph()
paragraph.AddChild(textRun)
' Create a new Word document
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("document.docx")
首先,我们在示例代码中导入 IronWord 库及其模型。
然后,我们创建一个新的 "文本 "变量。 该变量允许我们在 Word 文档中添加文本。
然后,我们将 "文本 "添加到 "段落 "中。这样,我们就可以通过区分两者来轻松处理文本。
然后,我们将 "段落 "参数传递给一个新的 "WordDocument "类,以创建一个新的文档对象。
最后,我们将文档保存为 Word 文件中的 "document.docx"。
在上一个例子中,我们创建了一个包含基本文本的 Word 文档。 让我们以更高级的功能为例,如自定义文本和添加文本特效。
using IronWord;
using IronWord.Models;
#region
IronWord.License.LicenseKey = "YOUR-KEY";
#endregion
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
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;
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
using IronWord;
using IronWord.Models;
#region
IronWord.License.LicenseKey = "YOUR-KEY";
#endregion
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
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;
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
#Region ""
IronWord.License.LicenseKey = "YOUR-KEY"
'#End Region
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
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
Dim paragraph As New Paragraph()
' Add text
paragraph.AddText(introText)
paragraph.AddText(italicText)
paragraph.AddText(boldText)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")
与上面的示例一样,我们创建了 "文本 "对象,并为文档添加了示例文本。
然后,我们创建一个新的 "TextStyle "对象,并将属性 "IsItalic "赋值为 true,表示文本应为斜体。
我们还对 "boldedText "变量进行了翻译,为属性 "IsBold "赋值为 true。
然后,我们将 "TextStyle "变量分配给各自的 "Text "变量。
然后,我们创建一个 "Paragraph "变量,并调用 "AddText "方法添加斜体和粗体文本。
最后,我们使用 "AddParagraph "方法将段落添加到文档中。
然后将文档保存为 "save_document.docx"。
上面的示例展示了开发人员使用 IronWord 可以使用的字体和样式,除了斜体和粗体文字效果。 IronWord 还提供了其他功能,以确保开发人员拥有在所有场景下创建独特 Word 文档的所有选项。
我们的示范展示了如何轻松使用IronWord用 C# 编程创建 Word 文档的库。 该库的灵活性和可扩展性使其成为开发人员在实际应用场景中的重要工具,例如在 Word 文档中自定义文本、字体和样式。 了解 Word 如何与其他应用程序集成,为开发人员解决难题提供更多解决方案。
IronWord提供了免费试用许可证.