• 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("	Start 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("		Contents of Cell:{0} = "{1}"", cellIndex, cellText);
            }
            //Console.WriteLine();
            Console.WriteLine("	End 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");

    作者:xuejianxiyang
    出处:http://xuejianxiyang.cnblogs.com
    关于作者:Heaven helps those who help themselves.
    本文版权归原作者和博客园共有,欢迎转载,但未经原作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    leetcode
    leetcode
    leetcode
    leetcode
    Postman
    Fiddler抓百度和bing搜索包
    Jmeter脚本录制
    Android Studio使用Maven国内镜像
    Open Live Writer
    PureXXX使用手记
  • 原文地址:https://www.cnblogs.com/xuejianxiyang/p/4862065.html
Copyright © 2020-2023  润新知