Aspose.Words for .NET使用表格教程之创建表格——创建表格的方法
Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
【下载Aspose.Words for .NET最新试用版】
接下来我们将进入“使用格式”的介绍,其中包括应用格式、介绍和创建表、添加和拆分表以及使用列和行。
表是word文档中常见的元素。它们允许在具有行和列的网格结构中清晰地组织和显示大量信息。它们还经常用作页面布局工具,并且是显示选项卡数据(带有选项卡停止)的更好选择,因为它们允许更好地控制内容的设计和布局。
表由Cell,Row和Column等元素组成。这些概念通常适用于所有表,无论它们来自Microsoft Word文档还是HTML文档,完全支持Aspose.Words中的表。您可以自由编辑,更改,添加和删除表格。还支持高保真表格的渲染。
创建表
使用DocumentBuilder插入表
在Aspose.Words中,通常使用DocumentBuilder插入表。以下方法用于构建表。其他方法也将用于将内容插入表格单元格。
- DocumentBuilder.StartTable:开始在当前光标位置构建新表。该表创建为空,并且还没有行或单元格。
- DocumentBuilder.InsertCell:在表中插入新的行和单元格。
- DocumentBuilder.EndRow:指示构建器结束当前行并在下一次调用DocumentBuilder.InsertCell时开始新行。
- DocumentBuilder.EndTable:构建器游标现在将指向表外部,准备在表之后插入内容。
- DocumentBuilder.Writeln:将一些文本写入当前单元格(现在是第二个单元格)。
下面的示例演示如何使用具有默认格式的DocumentBuilder创建简单表。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// 我们称这种方法开始构建表.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");
//构建第二个单元格
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content.");
//调用以下方法结束行并开始新行。
builder.EndRow();
//构建第二行的第一个单元格。
builder.InsertCell();
builder.Write("Row 2, Cell 1 Content");
//构建第二个单元格。
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content.");
builder.EndRow();
//表示我们已经完成了构建表的信号。
builder.EndTable();
dataDir = dataDir + "DocumentBuilder.CreateSimpleTable_out.doc";
// 将文档保存到磁盘。
doc.Save(dataDir);下面的示例演示如何使用DocumentBuilder创建格式化表。
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();
dataDir = dataDir + "DocumentBuilder.CreateFormattedTable_out.doc";
//将文档保存到磁盘。
doc.Save(dataDir);下面的示例演示如何使用DocumentBuilder插入嵌套表。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// 构建外部表。
Cell cell = builder.InsertCell();
builder.Writeln("Outer Table Cell 1");
builder.InsertCell();
builder.Writeln("Outer Table Cell 2");
//为了在第一个表中创建嵌套表,此调用很重要
//如果没有此调用,下面插入的单元格将附加到外部表格。
builder.EndTable();
// 移动到外部表的第一个单元格。
builder.MoveTo(cell.FirstParagraph);
//构建内部表
builder.InsertCell();
builder.Writeln("Inner Table Cell 1");
builder.InsertCell();
builder.Writeln("Inner Table Cell 2");
builder.EndTable();
dataDir = dataDir + "DocumentBuilder.InsertNestedTable_out.doc";
//将文档保存到磁盘。
doc.Save(dataDir);▲将表直接插入文档对象模型
要将表直接插入到特定节点位置的DOM中,使用DocumentBuilder创建表时使用相同的表默认值。 要在不使用DocumentBuilder的情况下从头构建新表,首先使用适当的构造函数创建一个新的Table 节点,然后将其添加到文档树中。
下面的示例演示如何使用节点的构造函数插入表。
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_WorkingWithTables(); Document doc = new Document(); //我们从创建表对象开始。请注意我们必须如何传递文档对象 // 到每个节点的构造函数。这是因为我们创建的每个节点都必须属于 //对某些文件 Table table = new Table(doc); //将表添加到文档中。 doc.FirstSection.Body.AppendChild(table); // Here we could call EnsureMinimum to create the rows and cells for us. This method is used // To ensure that the specified node is valid, in this case a valid table should have at least one // Row and one cell, therefore this method creates them for us. // Instead we will handle creating the row and table ourselves. This would be the best way to do this // If we were creating a table inside an algorthim for example. Row row = new Row(doc); row.RowFormat.AllowBreakAcrossPages = true; table.AppendChild(row); //我们现在可以应用任何自动调整设置。 table.AutoFit(AutoFitBehavior.FixedColumnWidths); //创建一个单元格并将其添加到行中 Cell cell = new Cell(doc); cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue; cell.CellFormat.Width = 80; // 为单元格添加一个段落以及带有一些文本的新运行。 cell.AppendChild(new Paragraph(doc)); cell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 1 Text")); //将单元格添加到行中。 row.AppendChild(cell); //然后,我们将对表中的其他单元格和行重复此过程。 //我们还可以通过克隆现有的单元格和行来加快速度。 row.AppendChild(cell.Clone(false)); row.LastCell.AppendChild(new Paragraph(doc)); row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text")); dataDir = dataDir + "Table.InsertTableUsingNodes_out.doc"; //将文档保存到磁盘。 doc.Save(dataDir);
▲插入现有表的克隆
通常有时您在文档中有现有表并希望添加此表的副本然后应用一些修改。在保留所有格式的同时复制表的最简单方法是使用Table.Clone方法克隆表节点。下面的示例演示如何使用节点的构造函数插入表。
Document doc = new Document(dataDir + "Table.SimpleTable.doc"); //检索文档中的第一个表 Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //创建表的克隆。 Table tableClone = (Table)table.Clone(true); //将克隆表插入原始文档后的文档中 table.ParentNode.InsertAfter(tableClone, table); //在两个表之间插入一个空段,否则它们将合并为一个 // Upon save. This has to do with document validation. table.ParentNode.InsertAfter(new Paragraph(doc), table); dataDir = dataDir + "Table.CloneTableAndInsert_out.doc"; //将文档保存到磁盘。 doc.Save(dataDir);
▲从HTML插入表
Aspose.Words支持使用DocumentBuilder.InsertHtml方法将内容从HTML源插入到文档中。输入可以是完整的HTML页面,也可以只是部分代码段。使用此方法,我们可以使用表元素(例如
,,
| )将表插入到文档中。下面的示例演示如何从包含HTML标记的字符串中插入文档中的表。 |
//文档目录的路径。
string dataDir = RunExamples.GetDataDir_WorkingWithTables();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
//从HTML插入表格。请注意,AutoFitSettings不适用于表
//从HTML插入
builder.InsertHtml("" +
"" +
"Row 1, Cell 1" +
"Row 1, Cell 2" +
"" +
"" +
"Row 2, Cell 2" +
"Row 2, Cell 2" +
"" +
"");
dataDir = dataDir + "DocumentBuilder.InsertTableFromHtml_out.doc";
//将文档保存到磁盘。
doc.Save(dataDir);*想要获取Aspose.Words正版授权可联系哦~
ASPOSE技术交流QQ群已开通,各类资源及时分享,欢迎交流讨论!(扫描下方二维码加入群聊)



QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线