Aspose.Words.Tables.Row类操作word表格行

http://www.aspose.com/docs/display/wordsnet/Aspose.Words.Tables.Row+Class


Retrieves the index of a row in a table.

获得行索引

[C#]

int rowIndex = table.IndexOf(row);

Shows how to make a clone of the last row of a table and append it to the table.

克隆最后一行并添加到表格

[C#]

Document doc = new Document(MyDir + "Table.SimpleTable.doc");

// Retrieve the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// Clone the last row in the table.
Row clonedRow = (Row)table.LastRow.Clone(true);

// Remove all content from the cloned row's cells. This makes the row ready for
// new content to be inserted into.
foreach (Cell cell in clonedRow.Cells)
    cell.RemoveAllChildren();

// Add the row to the end of the table.
table.AppendChild(clonedRow);

doc.Save(MyDir + "Table.AddCloneRowToTable Out.doc");

Shows how to iterate through all tables in the document and display the content from each cell.

遍历所有表格并显示每个单元格的内容

[C#]

Document doc = new Document(MyDir + "Table.Document.doc");

// Here we get all tables from the Document node. You can do this for any other composite node
// which can contain block level nodes. For example you can retrieve tables from header or from a cell
// containing another table (nested tables).
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);

// Iterate through all tables in the document
foreach (Table table in tables)
{
    // Get the index of the table node as contained in the parent node of the table
    int tableIndex = table.ParentNode.ChildNodes.IndexOf(table);
    Console.WriteLine("Start of Table {0}", tableIndex);

    // Iterate through all rows in the table
    foreach (Row row in table.Rows)
    {
        int rowIndex = table.Rows.IndexOf(row);
        Console.WriteLine("\tStart of Row {0}", rowIndex);

        // Iterate through all cells in the row
        foreach (Cell cell in row.Cells)
        {
            int cellIndex = row.Cells.IndexOf(cell);
            // Get the plain text content of this cell.
            string cellText = cell.ToString(SaveFormat.Text).Trim();
            // Print the content of the cell.
            Console.WriteLine("\t\tContents of Cell:{0} = \"{1}\"", cellIndex, cellText);
        }
        //Console.WriteLine();
        Console.WriteLine("\tEnd of Row {0}", rowIndex);
    }
    Console.WriteLine("End of Table {0}", tableIndex);
    Console.WriteLine();
}

Shows how to build a nested table without using DocumentBuilder.

不用documentbuilder类创建嵌套表格

[C#]

public void NestedTablesUsingNodeConstructors()
{
    Document doc = new Document();

    // Create the outer table with three rows and four columns.
    Table outerTable = CreateTable(doc, 3, 4, "Outer Table");
    // Add it to the document body.
    doc.FirstSection.Body.AppendChild(outerTable);

    // Create another table with two rows and two columns.
    Table innerTable = CreateTable(doc, 2, 2, "Inner Table");
    // Add this table to the first cell of the outer table.
    outerTable.FirstRow.FirstCell.AppendChild(innerTable);

    doc.Save(MyDir + "Table.CreateNestedTable Out.doc");

    Assert.AreEqual(2, doc.GetChildNodes(NodeType.Table, true).Count); // ExSkip
}

/// <summary>
/// Creates a new table in the document with the given dimensions and text in each cell.
/// </summary>
private Table CreateTable(Document doc, int rowCount, int cellCount, string cellText)
{
    Table table = new Table(doc);

    // Create the specified number of rows.
    for (int rowId = 1; rowId <= rowCount; rowId++)
    {
        Row row = new Row(doc);
        table.AppendChild(row);

        // Create the specified number of cells for each row.
        for (int cellId = 1; cellId <= cellCount; cellId++)
        {
            Cell cell = new Cell(doc);
            row.AppendChild(cell);
            // Add a blank paragraph to the cell.
            cell.AppendChild(new Paragraph(doc));

            // Add the text.
            cell.FirstParagraph.AppendChild(new Run(doc, cellText));
        }
    }

    return table;
}

Shows how to insert a table using the constructors of nodes.

使用节点构造函数创建表格

[C#]

Document doc = new Document();

// We start by creating the table object. Note how we must pass the document object
// to the constructor of each node. This is because every node we create must belong
// to some document.
Table table = new Table(doc);
// Add the table to the document.
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);

// We can now apply any auto fit settings.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);

// Create a cell and add it to the row
Cell cell = new Cell(doc);
cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
cell.CellFormat.Width = 80;

// Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 1 Text"));

// Add the cell to the row.
row.AppendChild(cell);

// We would then repeat the process for the other cells and rows in the table.
// We can also speed things up by cloning existing cells and rows.
row.AppendChild(cell.Clone(false));
row.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text"));

doc.Save(MyDir + "Table.InsertTableUsingNodes Out.doc");

<think>好的,我现在需要解决用户的问题:如何使用Aspose.Words API设置Word文档中表格中指定的高度,并且需要示例代码。首先,我得回忆一下Aspose.Words的相关文档和功能。 用户提到了引用[1]到[4],其中引用[1]提到了创建表格,引用[4]提到了提取内容。但用户的问题是关于设置高,所以可能需要查找Aspose.Words表格属性相关的文档。 Aspose.Words表格结构通常是Document对象包含Table,Table由Row组成,Row包含Cell。要设置高,可能需要访问RowRowFormat属性。记得在Aspose.Words中,的高度可以通过Height和HeightRule属性来控制。HeightRule可能是一个枚举,比如Exactly或AtLeast,这样高可以固定或根据内容调整。 接下来,用户需要示例代码。我需要确保代码正确,使用C#,因为引用中的示例都是C#。可能需要先创建文档,添加表格,然后访问特定设置属性。例如: 1. 创建Document对象和DocumentBuilder。 2. 使用DocumentBuilder开始表格,插入和单元格。 3. 获取创建的表格,然后访问特定的。 4. 设置RowFormat.Height和RowFormat.HeightRule。 需要确认是否正确应用这些属性。例如,设置HeightRule为HeightRule.Exact,然后设置Height的值。同时,可能需要处理AllowBreakAcrossPages属性,防止跨页断开。 另外,用户可能需要知道如何设置不同的高度,比如第一和其他不同。所以在示例代码中,可能需要创建多,并分别设置它们的属性。 最后,确保代码语法正确,比如使用using Aspose.Words;,以及正确的和方法命名。检查是否有拼写错误,比如RowFormat是否正确,是否调用了正确的构造函数。 总结步骤: - 初始化Document和DocumentBuilder。 - 构建表格结构。 - 获取表格中的特定。 - 设置高和规则。 - 保存文档。 可能需要参考Aspose.Words的官方文档或API参考,确保示例代码的准确性。同时,注意是否需要处理表格的创建方式,比如使用DocumentBuilder的StartTable和EndTable方法,或者通过直接添加Table对象。 现在,编写示例代码,并解释每一步的作用。确保代码可运,并指出需要引用的Aspose.Words库。可能还需要提到使用Aspose.Words的命名空间,以及如何安装NuGet包。 可能遇到的问题:是否索引是从0开始?比如Rows[0]表示第一。需要确认Aspose.Words的索引方式。此外,Height的单位是什么?应该是点(points),这是Word中常用的单位。 最后,生成相关问题,围绕表格操作,如合并单元格、设置边框等,根据用户提供的引用,可能引用到其他功能如目录、邮件合并,但相关问题应集中在表格处理上。</think>以下是使用Aspose.Words设置Word文档中表格指定高度的详细步骤及示例代码: ### 通过Aspose.Words设置表格指定高度的步骤 1. **创建文档对象并初始化表格** 使用`Document`和`DocumentBuilder`构建表格基础结构。 2. **获取目标** 通过表格的`Rows`集合索引访问特定。 3. **设置高规则** - `RowFormat.HeightRule`: 定义高度规则(如固定值`HeightRule.Exactly`或自动调整`HeightRule.Auto`)。 - `RowFormat.Height`: 设置具体高度值(单位为点)。 4. **保存文档** 生成最终的Word文件。 ### 示例代码 ```csharp using Aspose.Words; using Aspose.Words.Tables; // 1. 初始化文档和文档构建器 Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // 2. 创建表格(33列) builder.StartTable(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { builder.InsertCell(); builder.Write($"{i + 1}, 列{j + 1}"); } builder.EndRow(); } builder.EndTable(); // 3. 获取表格对象 Table table = (Table)doc.GetChild(NodeType.Table, 0, true); // 4. 设置第一高度为固定值50点 Row firstRow = table.Rows[0]; firstRow.RowFormat.HeightRule = HeightRule.Exactly; firstRow.RowFormat.Height = 50; // 5. 设置第二高度为自动调整 Row secondRow = table.Rows[1]; secondRow.RowFormat.HeightRule = HeightRule.Auto; // 6. 保存文档 doc.Save("TableWithCustomRowHeight.docx"); ``` ### 代码说明 - **索引**:`table.Rows[0]`表示表格的第一(索引从0开始)。 - **高度规则**: - `HeightRule.Exactly`: 固定高度,内容超出时会裁剪。 - `HeightRule.AtLeast`: 最小高度,高随内容自动增加。 - `HeightRule.Auto`: 完全由内容决定高度[^1]。 - **单位**:`Height`属性以**点(Points)**为单位(1点≈1/72英寸)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值