当前位置:网站首页>Aspose. Word operation word document (II)

Aspose. Word operation word document (II)

2022-07-07 22:22:00 Hey, hey, hey, hey, hey

Catalog

1. Create a table

2. Add data to the table

3. Get a table

4. Add a line to the table

5. Delete table rows

6. Delete a column of the table

7. Assign values to cells

8. merge cell


1. Create a table

        /// <summary>
        ///  Create a table 
        /// </summary>
        /// <param name="wordDoc"></param>
        /// <param name="rowCount"> Row number </param>
        /// <param name="cellCount"> Number of columns </param>
        /// <param name="ln"> Newline creation </param>
        /// <param name="alignment"> Paragraph alignment </param>
        public void CreateTable(Document wordDoc, int rowCount, int cellCount, bool ln, ParagraphAlignment alignment = ParagraphAlignment.Left)
        {
            DocumentBuilder builder = new DocumentBuilder(wordDoc);
            builder.MoveToDocumentEnd();
            if (ln) builder.Writeln();
            ParagraphFormat paragraph = builder.ParagraphFormat;
            paragraph.Alignment = alignment;
            builder.StartTable();
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < cellCount; j++)
                {
                    builder.InsertCell();
                }
                builder.EndRow();
            }
            builder.EndTable();
            paragraph.Alignment = ParagraphAlignment.Left;
        }

2. Add data to the table

         /// <summary>
        ///  Add data to the table 
        /// </summary>
        /// <param name="dt"></param>
        public void WriteTable(DataTable dt)
        {
            wordDoc.MailMerge.ExecuteWithRegions(dt);
        }

Example :

 

Private void AddValueToTable()
{
    DataTable dt = new DataTable(“StudentInfo”);
    dt.Columns.Add(“Name”);
    dt.Columns.Add(“Age”);
    dt.Columns.Add(“Sex”);
    dt.Rows.Add(new object[]{ “ Zhang San ”,”18”,” male ” });
    WriteTable(dt);
}

3. Get a table

         /// <summary>
        ///  Get form 
        /// </summary>
        /// <param name="tableIndex"></param>
        /// <returns></returns>
        public Table GetTable(int tableIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            return table;
        }

4. Add a line to the table

        /// <summary>
        ///  Add a row to the table 
        /// </summary>
        /// <param name="tableIndex"> The first few forms </param>
        public void AddRowItem(int tableIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            Row row = (Row)table.LastRow.Clone(true);
            table.AppendChild(row);
        }

5. Delete table rows

        /// <summary>
        ///  Delete the last row of the table 
        /// </summary>
        /// <param name="tableIndex"></param>
        public void RemoveLastRow(int tableIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            Row row = (Row)table.LastRow;
            table.RemoveChild(row);
        }
        /// <summary>
        ///  Delete the first row of the table 
        /// </summary>
        /// <param name="tableIndex"></param>
        public void RemoveFirstRow(int tableIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            Row row = (Row)table.LastRow;
            table.RemoveChild(row);
        }
        /// <summary>
        ///  Delete a row in the table 
        /// </summary>
        /// <param name="tableIndex"></param>
        /// <param name="rowIndex"></param>
        public void RemoveRowItem(int tableIndex, int rowIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            Row row = (Row)table.Rows[rowIndex];
            if(row != null)
                table.RemoveChild(row);
        }

6. Delete a column of the table

        /// <summary>
        ///  Delete a column of the table 
        /// </summary>
        /// <param name="tableIndex"></param>
        /// <param name="cellIndex"></param>
        public void RemoveCellItem(int tableIndex, int cellIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                Cell cell = table.Rows[i].Cells[cellIndex];
                if (cell != null)
                    table.Rows[i].RemoveChild(cell);
            }
        }

7. Assign values to cells

 /// <summary>
        ///  Set a cell value in the table 
        /// </summary>
        /// <param name="tableIndex"> form </param>
        /// <param name="row"> Lines </param>
        /// <param name="cell"> The first few columns </param>
        /// <param name="text"> value </param>
        public void SetCellValue(int tableIndex, int row, int cell, string text)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            Paragraph tempParagraph = table.Rows[row].Cells[cell].GetChild(NodeType.Paragraph, 0, true) as Paragraph;
            if (tempParagraph != null)
            {
                Run run = tempParagraph.GetChild(NodeType.Run, 0, true) as Run;
                if (run != null)
                {
                    run.Text = text;
                }
                else
                {
                    run = new Run(wordDoc);
                    run.Text = text;
                    tempParagraph.AppendChild(run);
                }
            }
            else
            {
                tempParagraph = new Paragraph(wordDoc);
                Run run = new Run(wordDoc);
                run.Text = text;
                tempParagraph.AppendChild(run);
            }
        }
        /// <summary>
        ///  Set the cell value of a row in the table 
        /// </summary>
        /// <param name="tableIndex"> form </param>
        /// <param name="row"> Lines </param>
        /// <param name="startCell"> Which column starts assignment </param>
        /// <param name="text"> value </param>
        public void SetCellValue(int tableIndex, int row,int startCell, string[] text)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            for (int i = startCell; i < table.Rows[row].Cells.Count; i++)
            {
                Paragraph tempParagraph = table.Rows[row].Cells[i].GetChild(NodeType.Paragraph, 0, true) as Paragraph;
                if (tempParagraph != null)
                {
                    Run run = tempParagraph.GetChild(NodeType.Run, 0, true) as Run;
                    if (run != null)
                    {
                        run.Text = text[i];
                    }
                    else
                    {
                        run = new Run(wordDoc);
                        run.Text = text[i];
                        tempParagraph.AppendChild(run);
                    }
                }
                else
                {
                    tempParagraph = new Paragraph(wordDoc);
                    Run run = new Run(wordDoc);
                    run.Text = text[i];
                    tempParagraph.AppendChild(run);
                }
            }
            
        }

8. merge cell

        /// <summary>
        ///  Merge cells vertically 
        /// </summary>
        /// <param name="tableIndex"> The first few forms </param>
        /// <param name="startRow"> The subscript of the starting line to be merged </param>
        /// <param name="endRow"> The subscript of the end line to be merged </param>
        /// <param name="CellIndex"> Which column subscripts do you want to merge </param>
        public void MergeCells(int tableIndex, int startRow, int endRow, int CellIndex)
        {
            if (endRow - startRow < 1) return;
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            table.Rows[startRow].Cells[CellIndex].CellFormat.VerticalMerge = CellMerge.First;
            for (int i = startRow + 1; i <= endRow; i++)
            {
                table.Rows[i].Cells[CellIndex].CellFormat.VerticalMerge = CellMerge.Previous;
            }
        }

        /// <summary>
        ///  Merge cells horizontally 
        /// </summary>
        /// <param name="tableIndex"> The first few forms </param>
        /// <param name="startRow"> The subscript of the starting column to be merged </param>
        /// <param name="endRow"> The subscript of the end column to be merged </param>
        /// <param name="CellIndex"> Which line of subscripts do you want to merge </param>
        public void MergeCells(int tableIndex, int startCell, int endCell, int rowIndex)
        {
            if (endRow - startRow < 1) return;
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            table.Rows[rowIndex].Cells[startCell].CellFormat.HorizontalMerge = CellMerge.First;
            for (int i = startCell + 1; i <= endCell; i++)
            {
                table.Rows[rowIndex].Cells[i].CellFormat.HorizontalMerge = CellMerge.Previous;
            }
        }

原网站

版权声明
本文为[Hey, hey, hey, hey, hey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130606206740.html