使用IRONWORD

如何在C#中创建不使用Office Interop的Word文档

发布 2024年三月12日
分享:

Microsoft Word 文档被广泛用于各种用途,从正式的商业报告到私人信件。在 C# 中,开发人员经常需要以编程方式生成 Microsoft Word 文档。Windows 应用程序开发人员历来使用 Microsoft Office Interop 来使用 C# 生成和创建 Word 文档。

然而,并非所有人都能使用这种方法,开发人员使用的操作系统或 Linux 机器上可能没有 Microsoft Office 界面。在这种情况下,开发人员必须探索能在不同平台上独立运行的其他库。其中一个功能强大的用于以编程方式处理 Word 文件的库是 铁字 来自 铁软件.

IronWord 为在.NET 应用程序中处理 Word 文档提供了强大的功能,可以在不同的平台和基于 Linux 的 docker 映像/容器上运行。IronWord 具有直观的 C#、VB.NET Word 和 Docx 文档 API,因此无需安装 Microsoft Office、Office 自动化或 Word Interop,即可构建、编辑和导出 Word 文档文件。IronWord 完全支持 .NET 8、7、6、Framework、Core 和 Azure。

本文将探讨如何使用 IronWord 库在 C# 中创建 Word 文档。

如何在不使用 Office Interop 的情况下在 C&num 中创建 Word 文档;

1.创建一个新的 C# 项目。

2.安装 铁字 图书馆

3.使用 IronWord 库创建 Word 文档。

4.向现有文档添加内容。

5.保存创建的 Word 文档。

6.打开并显示创建的 Word 文档。

前提条件:

  1. Visual Studio: 确保安装了 Visual Studio 或其他 C# 开发环境。

  2. NuGet软件包管理器: 确保可以使用NuGet管理项目中的软件包

第 1 步:创建新的 C#项目

创建一个新的 C# 控制台应用程序或使用现有项目来生成 Word 文档。

选择控制台应用程序模板,然后单击下一步。

如何在不使用 Office Interop 的情况下用 C# 创建 Word 文档:图 1 - 用于创建新项目的控制台应用程序模板

下一步,您可以提供解决方案和项目名称。

如何在不使用 Office Interop 的情况下用 C# 创建 Word 文档:图 2 - 用解决方案和项目名称配置项目

选择 .NET 版本并点击 "创建"。

如何在不使用 Office Interop 的情况下用 C#.NET 创建 Word 文档?图 3 - 使用正确的 .NET 版本创建项目

第 2 步:安装 IronWord 库

打开 C# 项目并安装 IronWord 库:

Install-Package IronWord -Version 2024.1.2

还可以使用 Visual Studio 包管理器安装 NuGet 包,如下所示。

如何在不使用 Office Interop 的情况下用 C# 创建 Word 文档:图 4 - 使用 VS 软件包管理器安装 IronWord

步骤 3: 使用 IronWord 库创建并保存 Word 文档

让我们从使用 IronWord 库创建 Word 文档的简单示例开始。下面的代码演示了如何创建一个基本的 Word 文档,其中只有一个段落,包含文本 "Hello, World!"

using IronWord;
using IronWord.Models;
// Create textrun instance
TextRun textRun = new TextRun("Hello, World!");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("example.docx"); // save docx file to disk
using IronWord;
using IronWord.Models;
// Create textrun instance
TextRun textRun = new TextRun("Hello, World!");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("example.docx"); // save docx file to disk
Imports IronWord
Imports IronWord.Models
' Create textrun instance
Private textRun As New TextRun("Hello, World!")
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a new Word document object
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("example.docx") ' save docx file to disk
VB   C#

执行上述代码示例后,创建了新的文档文件 example.docx,输出如下所示。

如何在不使用 Office Interop 的情况下用 C# 创建 Word 文档:图 5 - 通过上述代码创建的 Word 文档。

这是一个使用 IronWord 生成 Word 文档文件的基本示例。如需了解更多信息,请参阅以下文档 *这里***.

在 Word 文档中添加带样式的段落。

现在我们知道了如何使用 IronWord 创建一个简单的 Word 文档,让我们来添加段落和样式文本。

TextRun 还可以获取样式数据,增强文本的视觉效果。文本可以设置删除线、粗体、斜体和下划线等样式。

修改并添加以下代码到您之前制作的程序中。

using IronWord;
using IronWord.Models;
// Create textrun
TextRun textRun = new TextRun("Hello, World!"); // add string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); // add string or text
TextStyle italicStyle = new TextStyle()
{
    IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle); 
// add italic string
TextStyle boldStyle = new TextStyle()
{
    IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle); 
// add bold string
// Add text
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx document
doc.SaveAs("example.docx");// docx file save in EXE location
using IronWord;
using IronWord.Models;
// Create textrun
TextRun textRun = new TextRun("Hello, World!"); // add string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); // add string or text
TextStyle italicStyle = new TextStyle()
{
    IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle); 
// add italic string
TextStyle boldStyle = new TextStyle()
{
    IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle); 
// add bold string
// Add text
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx document
doc.SaveAs("example.docx");// docx file save in EXE location
Imports IronWord
Imports IronWord.Models
' Create textrun
Private textRun As New TextRun("Hello, World!") ' add string
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Configure text
Dim introText As New TextRun("This is an example paragraph with italic and bold styling.") ' add string or text
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New TextRun("Italic example sentence.", italicStyle)
' add italic string
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New TextRun("Bold example sentence.", boldStyle)
' add bold string
' Add text
paragraph.AddTextRun(introText)
paragraph.AddTextRun(italicText)
paragraph.AddTextRun(boldText)
' Create a new Word document
Dim doc As New WordDocument(paragraph)
' Export docx document
doc.SaveAs("example.docx") ' docx file save in EXE location
VB   C#

在 Word 文档中添加表格

为了清晰地表示数据,也可以用网格来表示数据。当数据以网格形式排列时,就称为表格。使用 IronWord,我们可以在 Word 文档中添加表格和图像,如下图所示:

using IronWord;
using IronWord.Models;
// Create table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Sample text";
// Add textrun to the cell
cell.AddContent(new Paragraph(textRun));
// Configure border style 
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = new IronColor(IronSoftware.Drawing.Color.Black);
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;
// Configure table border
TableBorders tableBorders = new TableBorders() { 
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};
cell.Borders = tableBorders;
// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell); // add cell
row.AddCell(cell); // add cell
// Create table and add row
Table table = new Table();
table.AddRow(row);
// Generate document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx"); // docx file save in EXE location
using IronWord;
using IronWord.Models;
// Create table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Sample text";
// Add textrun to the cell
cell.AddContent(new Paragraph(textRun));
// Configure border style 
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = new IronColor(IronSoftware.Drawing.Color.Black);
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;
// Configure table border
TableBorders tableBorders = new TableBorders() { 
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};
cell.Borders = tableBorders;
// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell); // add cell
row.AddCell(cell); // add cell
// Create table and add row
Table table = new Table();
table.AddRow(row);
// Generate document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx"); // docx file save in EXE location
Imports IronWord
Imports IronWord.Models
' Create table cell
Private cell As New TableCell()
Private textRun As New TextRun()
textRun.Text = "Sample text"
' Add textrun to the cell
cell.AddContent(New Paragraph(textRun))
' Configure border style 
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = New IronColor(IronSoftware.Drawing.Color.Black)
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick
borderStyle.BorderSize = 5
' Configure table border
Dim tableBorders As New TableBorders() With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}
cell.Borders = tableBorders
' Create row and add cell
Dim row As New TableRow()
row.AddCell(cell) ' add cell
row.AddCell(cell) ' add cell
' Create table and add row
Dim table As New Table()
table.AddRow(row)
' Generate document from the table
Dim doc As New WordDocument(table)
' Export Word document
doc.SaveAs("Document.docx") ' docx file save in EXE location
VB   C#

在上面的示例中,我们使用边框在 Word 文档中添加了一个表格。

在 Word 文档中添加图像

图片可以增强文档的表现力,增加色彩和视觉吸引力。

图像可以使用 IronWord 以编程方式添加到 Word 文档中,如下代码所示:

using IronWord;
using IronWord.Models;
// Load new document
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg");
image.Width = 200; // In unit pixel (px)
image.Height = 200; // In unit pixel (px)
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph 
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx"); // docx file
using IronWord;
using IronWord.Models;
// Load new document
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg");
image.Width = 200; // In unit pixel (px)
image.Height = 200; // In unit pixel (px)
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph 
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx"); // docx file
Imports IronWord
Imports IronWord.Models
' Load new document
Private doc As New WordDocument()
' Configure image
Private image As New IronWord.Models.Image("SalesChart.jpg")
image.Width = 200 ' In unit pixel (px)
image.Height = 200 ' In unit pixel (px)
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph 
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx") ' docx file
VB   C#

在这里,我们使用 IronWord.Models.Image 为段落添加一张高度和宽度均为 200 像素的图片。

许可 (可免费试用)

使用 IronWord 需要许可证。试用密钥可从 Iron 软件网站获取 这里.此密钥需要放在 appsettings.json 中。

{
    "IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
}
{
    "IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
}
If True Then
	"IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
End If
VB   C#

提供电子邮件以获取试用许可证。提交电子邮件 ID 后,密钥将通过电子邮件发送。

如何在不使用 Office Interop 的情况下用 C# 创建 Word 文档:图 6 - 成功提交试用表单后的弹出窗口

结论

使用 铁字 IronWord 库提供了一种无需依赖 Microsoft Office 即可生成文档的灵活高效的方法。无论您需要创建简单的信件还是带有表格和图像的复杂报告,IronWord 都能让您通过编程来实现。本文提供了创建 Word 文档的全面教程。有了 IronWord,您就可以自动创建 Word 文档,使您的 C# 应用程序功能更全面、效率更高。

如果开发人员希望在生成 Word 文档的同时处理 PDF 文件,那么 Iron Software 开发的另一个 C# 库 IronPDF 将是最佳选择。如需查看,请点击 *这里***.

< 前一页
如何在VB . NET中以编程方式创建Word文档
下一步 >
如何在C#中读取带格式的Word文档

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 4,816 查看许可证 >