当前位置 : 主页 > 编程语言 > c语言 >

C# 利用Aspose.word导出word--表格篇

来源:互联网 收集:自由互联 发布时间:2021-06-25
一、直接插入表格 private void InsertTable(DocumentBuilder builder, Document doc) { // 表格列数 var cellcount = 4 ; // 表格开始编辑 Table tables = builder.StartTable(); // 表格行样式 builder.RowFormat.Alignment = RowAl

一、直接插入表格

      private void InsertTable(DocumentBuilder builder, Document doc)
        {
                //表格列数
                var cellcount = 4;
                //表格开始编辑
                Table tables = builder.StartTable();
                //表格行样式
                builder.RowFormat.Alignment = RowAlignment.Center;
                //表格边框样式
                builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                //单元格边框颜色
                builder.CellFormat.Borders.Color = Color.Black;
                builder.Font.Size = 10.5;//字体10.5磅
                builder.Bold = true;//加粗

                for (var i = 0; i < cellcount; i++)
                {
                    //插入单元格
                    builder.InsertCell();
                    //单元格背景颜色
                    builder.CellFormat.Shading.BackgroundPatternColor = Color.LightYellow;
                    //单元格内字体对齐方式
                    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                    if (i == 0)
                    {
                        builder.Write("单元格1");
                    }
                    else if (i == 1)
                    {
                        builder.Write("单元格2");
                    }
                    else if (i == 2)
                    {
                        builder.Write("单元格3");
                    }
                    else if (i == 3)
                    {
                        builder.Write("单元格4");
                    }
                }
         //行编辑结束
         builder.EndRow();
//表格结束编辑 builder.EndTable(); }

二、获取模板表格

 

      private void InsertTable(DocumentBuilder builder, Document doc)
        {
                builder.StartTable();
                NodeCollection nodes = builder.Document.GetChildNodes(NodeType.Table, true);
                if (nodes.Count == 0)
                {
                    return;
                }
                Table table = (Table)nodes[1];//获取目标表格
                CellCollection cellTmp = table.Rows[0].Cells;//单元格列
                var cellcount = cellTmp.Count;//列数
                Row row;

                for (var i = 0; i < cellcount; i++)
                {
                    //插入单元格
                    builder.InsertCell();

                    if (i == 0)
                    {
                        builder.Write("单元格1");
                    }
                    else if (i == 1)
                    {
                        builder.Write("单元格2");
                    }
                    else if (i == 2)
                    {
                        builder.Write("单元格3");
                    }
                    else if (i == 3)
                    {
                        builder.Write("单元格4");
                    }
                }  
          //行编辑结束 row
= builder.EndRow(); table.AppendChild(row);           //表格结束编辑 builder.EndTable(); }

二、设置表格单元格长度

          builder.StartTable();
                //插入由三个具有不同首选宽度的单元格组成的表行。

                //插入一个绝对大小的单元格。
                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(40);
                builder.CellFormat.Shading.BackgroundPatternColor = Color.LightYellow;
                builder.Writeln("Cell at 40 points width");

                //插入一个相对大小(百分比)的单元格。
                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(20);
                builder.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
                builder.Writeln("Cell at 20% width");

                //插入一个自动大小的单元格。
                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.Auto;
                builder.CellFormat.Shading.BackgroundPatternColor = Color.LightGreen;
                builder.Writeln("Cell automatically sized. The size of this cell is calculated from the table preferred width.");
                builder.Writeln("In this case the cell will fill up the rest of the available space.");

                builder.EndTable();

三、单元格合并行(builder.CellFormat.HorizontalMerge )

                    for (var i = 0; i < 4; i++)
                    {
                        
                        builder.InsertCell();
                        if (i == 0)
                        {
                            builder.Write("单元格1");
                //列合并开始  builder.CellFormat.HorizontalMerge = CellMerge.First;
} else if (i > 1) {
                 //列合并 builder.CellFormat.HorizontalMerge
= CellMerge.Previous; } }
            builder.EndRow();

四、单元格合并列(builder.CellFormat.VerticalMerge)

             for (var i = 0; i < 4; i++)
                    {                        
                        builder.InsertCell();
               builder.CellFormat.HorizontalMerge = CellMerge.None;
                        builder.CellFormat.VerticalMerge = CellMerge.None;
                        if (i == 0)
                        {
                            builder.Write("单元格1");
                        }
                        else if (i == 3)
                        {
            //行合并开始
            builder.CellFormat.VerticalMerge = CellMerge.First;
                    builder.Write("单元格3");
                        }
                        else if (i > 1)
                        {
                    builder.Write("单元格");
                        }
                    }
            for (var i = 0; i < 4; i++)
                    {                        
                        builder.InsertCell();
                        if (i == 0)
                        {
                            builder.Write("单元格1");
                        }
                        else if (i == 3)
                        {
          //合并行
          builder.CellFormat.VerticalMerge = CellMerge.Previous;
                        }
                        else if (i > 1)
                        {
                    builder.Write("单元格");
                        }
                    }
网友评论