Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
接下来我们将进入“使用格式”的介绍,其中包括应用格式、介绍和创建表、添加和拆分表以及使用列和行。本文将为大家讲解如何在表格中运用自动调整设置。
>>Aspose.Words for .NET更新至最新版,欢迎下载体验
欢迎下载|体验更多Aspose文档管理产品 或 加入Aspose技术交流群(761297826)
在一个表中,几个单元格可以合并到一个单元格中。当某些行需要标题或跨表宽度的大块文本时,此功能很有用。这只能通过将表中的某些单元格合并为一个单元格来实现。当使用所有输入格式(包括导入HTML内容)时,Aspose.Words支持合并的单元格。
Aspose.Words中的合并单元格
在Aspose.Words中,合并的单元格由CellFormat.HorizontalMerge和CellFormat.VerticalMerge表示。所述CellFormat.HorizontalMerge属性描述如果所述细胞是细胞的水平合并的一部分。同样,CellFormat.VerticalMerge属性描述单元格是否是单元格垂直合并的一部分。这些属性的值定义了单元格的合并行为。
- 合并单元格序列中的第一个单元格将具有CellMerge.First。
- 任何随后合并的单元格将具有CellMerge.Previous。
- 未合并的单元格将具有CellMerge.None。
检查单元格是否合并
要检查某个单元格是否为合并单元格序列的一部分,我们只需检查CellFormat.HorizontalMerge和CellFormat.VerticalMerge属性。 下面的示例显示单元格的水平和垂直合并类型。您可以从此处下载该示例的模板文件。
Document doc = new Document(dataDir + "Table.MergedCells.doc");
// Retrieve the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
foreach (Row row in table.Rows)
{
foreach (Cell cell in row.Cells)
{
Console.WriteLine(PrintCellMergeType(cell));
}
}
合并表格中的单元格
使用相同的技术来设置表中单元格的合并行为。使用DocumentBuilder构建具有合并单元格的表时,需要为每个单元格设置适当的合并类型。另外,您必须记住清除合并设置,否则表中的所有单元格将被合并。可以通过将适当的合并属性的值设置为CellMerge.None来完成。 下面的示例创建一个具有两行的表,其中第一行的单元格水平合并。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.Write("Text in merged cells.");
builder.InsertCell();
// This cell is merged to the previous and should be empty.
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Write("Text in one cell.");
builder.InsertCell();
builder.Write("Text in another cell.");
builder.EndRow();
builder.EndTable();
dataDir = dataDir + "Table.HorizontalMerge_out.doc";
// Save the document to disk.
doc.Save(dataDir);
下面的示例创建一个具有两列的表格,第一列中的单元格垂直合并。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertCell();
builder.CellFormat.VerticalMerge = CellMerge.First;
builder.Write("Text in merged cells.");
builder.InsertCell();
builder.CellFormat.VerticalMerge = CellMerge.None;
builder.Write("Text in one cell");
builder.EndRow();
builder.InsertCell();
// This cell is vertically merged to the cell above and should be empty.
builder.CellFormat.VerticalMerge = CellMerge.Previous;
builder.InsertCell();
builder.CellFormat.VerticalMerge = CellMerge.None;
builder.Write("Text in another cell");
builder.EndRow();
builder.EndTable();
dataDir = dataDir + "Table.VerticalMerge_out.doc";
// Save the document to disk.
doc.Save(dataDir);
在不使用构建器的其他情况下(例如在现有表中),以这种方式合并单元可能不是那么简单。 取而代之的是,我们可以将将合并属性应用于单元格所涉及的基本操作包装到一种使任务轻松得多的方法中。 此方法类似于自动合并方法,该方法被称为合并表中的一系列单元格。 下面的代码将合并表中从给定单元格到结束单元格的单元格范围。 此范围可以跨越许多行或列。 一种合并表中所有单元格在指定单元格范围内的方法。
internal static void MergeCells(Cell startCell, Cell endCell)
{
Table parentTable = startCell.ParentRow.ParentTable;
// Find the row and cell indices for the start and end cell.
Point startCellPos = new Point(startCell.ParentRow.IndexOf(startCell), parentTable.IndexOf(startCell.ParentRow));
Point endCellPos = new Point(endCell.ParentRow.IndexOf(endCell), parentTable.IndexOf(endCell.ParentRow));
// Create the range of cells to be merged based off these indices. Inverse each index if the end cell if before the start cell.
Rectangle mergeRange = new Rectangle( System.Math.Min(startCellPos.X, endCellPos.X), System.Math.Min(startCellPos.Y, endCellPos.Y),
System.Math.Abs(endCellPos.X - startCellPos.X) + 1, System.Math.Abs(endCellPos.Y - startCellPos.Y) + 1);
foreach (Row row in parentTable.Rows)
{
foreach (Cell cell in row.Cells)
{
Point currentPos = new Point(row.IndexOf(cell), parentTable.IndexOf(row));
// Check if the current cell is inside our merge range then merge it.
if (mergeRange.Contains(currentPos))
{
if (currentPos.X == mergeRange.X)
cell.CellFormat.HorizontalMerge = CellMerge.First;
else
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
if (currentPos.Y == mergeRange.Y)
cell.CellFormat.VerticalMerge = CellMerge.First;
else
cell.CellFormat.VerticalMerge = CellMerge.Previous;
}
}
}
}
下面的示例合并两个指定单元格之间的单元格范围。
// Open the document
Document doc = new Document(dataDir + "Table.Document.doc");
// Retrieve the first table in the body of the first section.
Table table = doc.FirstSection.Body.Tables[0];
// We want to merge the range of cells found inbetween these two cells.
Cell cellStartRange = table.Rows[2].Cells[2];
Cell cellEndRange = table.Rows[3].Cells[3];
// Merge all the cells between the two specified cells into one.
MergeCells(cellStartRange, cellEndRange);
dataDir = dataDir + "Table.MergeCellRange_out.doc";
// Save the document.
doc.Save(dataDir);