C#开发---Aspose.Word操作Word文档(二)

目录

1.创建表格

2.往表格中添加数据

3.获取某个表格

4.表格增加一行

5.删除表格行

6.删除表格某一列

7.给单元格赋值

8.合并单元格


1.创建表格

        /// 
        /// 创建表格
        /// 
        /// 
        /// 行数
        /// 列数
        /// 换行创建
        /// 段落对齐方式
        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.往表格中添加数据

         /// 
        /// 将数据添加到表格
        /// 
        /// 
        public void WriteTable(DataTable dt)
        {
            wordDoc.MailMerge.ExecuteWithRegions(dt);
        }

示例:

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[]{ “张三”,”18”,”男” });
    WriteTable(dt);
}

3.获取某个表格

         /// 
        /// 获取表格
        /// 
        /// 
        /// 
        public Table GetTable(int tableIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            return table;
        }

4.表格增加一行

        /// 
        /// 给表格添加一行
        /// 
        /// 第几个表格
        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.删除表格行

        /// 
        /// 删除表格最后一行
        /// 
        /// 
        public void RemoveLastRow(int tableIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            Row row = (Row)table.LastRow;
            table.RemoveChild(row);
        }
        /// 
        /// 删除表格第一行
        /// 
        /// 
        public void RemoveFirstRow(int tableIndex)
        {
            Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
            Row row = (Row)table.LastRow;
            table.RemoveChild(row);
        }
        /// 
        /// 删除表格某一行
        /// 
        /// 
        /// 
        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.删除表格某一列

        /// 
        /// 删除表格某一列
        /// 
        /// 
        /// 
        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.给单元格赋值

 /// 
        /// 设置表格某一个单元格值
        /// 
        /// 表格
        /// 第几行
        /// 第几列
        /// 值
        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);
            }
        }
        /// 
        /// 设置表格某一行单元格值
        /// 
        /// 表格
        /// 第几行
        /// 第几列开始赋值
        /// 值
        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.合并单元格

        /// 
        /// 竖向合并单元格
        /// 
        /// 第几个表格
        /// 要合并的起始行下标
        /// 要合并的结束行下标
        /// 要合并第几列下标
        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;
            }
        }

        /// 
        /// 横向合并单元格
        /// 
        /// 第几个表格
        /// 要合并的起始列下标
        /// 要合并的结束列下标
        /// 要合并第几行下标
        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;
            }
        }

你可能感兴趣的:(C#,c#,aspose)