格式化表
内容
[
隐藏
]在本教程中,我们将学习如何使用 Aspose.Words for .NET 在 Word 文档中创建格式化表格。我们将按照分步指南来理解代码并实现此功能。在本教程结束时,您将能够以编程方式在 Word 文档中创建具有自定义格式的表格。
第 1 步:项目设置
- 启动 Visual Studio 并创建一个新的 C# 项目。
- 添加对 Aspose.Words for .NET 库的引用。
第2步:创建文档并初始化文档生成器
要开始构建格式化表,我们需要创建一个新文档并初始化文档生成器。按着这些次序:
//文档目录的路径
string dataDir = "YOUR DOCUMENTS DIRECTORY";
//创建文档并初始化文档生成器
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
请务必将“您的文档目录”替换为文档目录的实际路径。
第 3 步:构建格式化表
接下来,我们将使用文档构建器提供的方法构建格式化表格。使用以下代码:
//开始数组构建
Table table = builder. StartTable();
//表头行的构造
builder. InsertCell();
table. LeftIndent = 20.0;
builder.RowFormat.Height = 40.0;
builder.RowFormat.HeightRule = HeightRule.AtLeast;
builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(198, 217, 241);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 16;
builder.Font.Name = "Arial";
builder.Font.Bold = true;
builder.CellFormat.Width = 100.0;
builder.Write("Header Row,\n Cell 1");
builder. InsertCell();
builder.Write("Header Row,\n Cell 2");
builder. InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Header Row,\n Cell 3");
builder. EndRow();
//阵体的构建
builder.CellFormat.Shading.BackgroundPatternColor = Color.White;
builder.CellFormat.Width = 100.0;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.RowFormat.Height = 30.0;
builder.RowFormat.HeightRule = HeightRule.Auto;
builder. InsertCell();
builder.Font.Size = 12;
builder.Font.Bold = false;
builder.Write("Content Line 1, Cell 1");
builder. InsertCell();
builder.Write("Content Line 1, Cell 2");
builder. InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Content Line 1, Cell
3");
builder. EndRow();
builder. InsertCell();
builder.CellFormat.Width = 100.0;
builder.Write("Content Line 2, Cell 1");
builder. InsertCell();
builder.Write("Content Line 2, Cell 2");
builder. InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Content Line 2, Cell 3");
builder. EndRow();
//数组构建结束
builder. EndTable();
这里我们使用文档构建器来一步步构建表格。我们首先调用StartTable()
初始化表。然后我们使用InsertCell()
插入细胞和Write()
向每个单元格添加内容。我们还使用不同的格式属性来定义表行、单元格和文本的格式。
步骤 4:保存文档
最后,我们需要保存包含格式化表格的文档。使用以下代码:
//保存文档
doc.Save(dataDir + "WorkingWithTables.FormattedTable.docx");
请务必为输出文档指定正确的路径和文件名。
使用 Aspose.Words for .NET 的格式化表的示例源代码
//文档目录的路径
string dataDir = "YOUR DOCUMENT DIRECTORY";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
//表中至少存在一行后,必须应用表宽格式。
table.LeftIndent = 20.0;
//设置高度并定义标题行的高度规则。
builder.RowFormat.Height = 40.0;
builder.RowFormat.HeightRule = HeightRule.AtLeast;
builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(198, 217, 241);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 16;
builder.Font.Name = "Arial";
builder.Font.Bold = true;
builder.CellFormat.Width = 100.0;
builder.Write("Header Row,\n Cell 1");
//我们不需要指定该单元格的宽度,因为它是从前一个单元格继承的。
builder.InsertCell();
builder.Write("Header Row,\n Cell 2");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Header Row,\n Cell 3");
builder.EndRow();
builder.CellFormat.Shading.BackgroundPatternColor = Color.White;
builder.CellFormat.Width = 100.0;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
//重置高度并为表体定义不同的高度规则。
builder.RowFormat.Height = 30.0;
builder.RowFormat.HeightRule = HeightRule.Auto;
builder.InsertCell();
//重置字体格式。
builder.Font.Size = 12;
builder.Font.Bold = false;
builder.Write("Row 1, Cell 1 Content");
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Row 1, Cell 3 Content");
builder.EndRow();
builder.InsertCell();
builder.CellFormat.Width = 100.0;
builder.Write("Row 2, Cell 1 Content");
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Row 2, Cell 3 Content.");
builder.EndRow();
builder.EndTable();
doc.Save(dataDir + "WorkingWithTables.FormattedTable.docx");
结论
在本教程中,我们学习了如何使用 Aspose.Words for .NET 在 Word 文档中创建格式化表格。通过遵循本分步指南并实施提供的 C# 代码,您可以以编程方式在 Word 文档中创建具有特定格式的自定义表格。此功能允许您以视觉上有吸引力且有组织的方式呈现和构建数据。