C#操作word之插入表格

假如我们需要在一个word文档的某个位置插入一张表格,而且要对这一张表格中的单元格做拆分合并,之类的。

先看下效果,先不管表格是否合理,总之就是要这样的统计文档,但是人数,班级数都是不确定的,也就是表格是根 据数据动态生成的,

这样我们就很难用之前的替换方式来实现,也就是要动态来创建表格。

                                                                                             班级成绩统计单

班级 姓名 成绩 人数总计 班主任
一班 张三 498 1 小李
二班 李四 354 2 陈飞
小红 502
三班 丁爽 566 1 王林

1、用单元格拆分的方式实现,也就是根据有多少班级,则增加多少行,针对于其中的学生信息再对单元格进行拆分。

 /// <summary>
        /// 数据实体类
        /// </summary>
        public class Student
        {
            public string Name;//姓名
            public int Score;//成绩
            public string StuClass;//班级
            public string Leader;//班主任
        } 
        /// <summary>
        /// 动态创建table到word
        /// </summary>
        protected void CreateTableToExcel()
        {
            Word.Application app = null;
            Word.Document doc = null;
            try
            {   
                //构造数据
                List<Student> datas = new List<Student>();
                datas.Add(new Student{ Leader="小李", Name="张三", Score=498, StuClass="一班"});
                datas.Add(new Student{ Leader="陈飞", Name="李四", Score=354, StuClass="二班"});
                datas.Add(new Student{ Leader="陈飞", Name="小红", Score=502, StuClass="二班"});
                datas.Add(new Student{ Leader="王林", Name="丁爽", Score=566, StuClass="三班"});
                var cate = datas.GroupBy(s=>s.StuClass);

                int rows = cate.Count()+1;//表格行数加1是为了标题栏
                int cols = 5;//表格列数
                object oMissing = System.Reflection.Missing.Value;
                app = new Word.Application();//创建word应用程序
                doc = app.Documents.Add();//添加一个word文档

                //输出大标题加粗加大字号水平居中
                app.Selection.Font.Bold = 700;
                app.Selection.Font.Size = 16;
                app.Selection.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                app.Selection.Text = "班级成绩统计单";

                //换行添加表格
                object line = Word.WdUnits.wdLine;
                app.Selection.MoveDown(ref line, oMissing, oMissing);
                app.Selection.TypeParagraph();//换行
                Word.Range range = app.Selection.Range;
                Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);

                //设置表格的字体大小粗细
                table.Range.Font.Size = 10;
                table.Range.Font.Bold=0;

                //设置表格标题
                int rowIndex = 1;
                table.Cell(rowIndex, 1).Range.Text = "班级";
                table.Cell(rowIndex, 2).Range.Text = "姓名";
                table.Cell(rowIndex, 3).Range.Text = "成绩";
                table.Cell(rowIndex, 4).Range.Text = "人数";
                table.Cell(rowIndex, 5).Range.Text = "班主任";

                //循环数据创建数据行
                rowIndex++;
                foreach (var i in cate)
                {
                    table.Cell(rowIndex, 1).Range.Text = i.Key;//班级
                    table.Cell(rowIndex, 4).Range.Text = i.Count().ToString();//人数
                    table.Cell(rowIndex, 5).Range.Text = i.First().Leader;//班主任
                    table.Cell(rowIndex,2).Split(i.Count(), 1);//分割名字单元格
                    table.Cell(rowIndex,3).Split(i.Count(), 1);//分割成绩单元格

                    //对表格中的班级、姓名,成绩单元格设置上下居中
                    table.Cell(rowIndex, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 4).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 5).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                    //构建姓名,成绩数据
                    foreach (var x in i)
                    {
                        table.Cell(rowIndex, 2).Range.Text = x.Name;
                        table.Cell(rowIndex, 3).Range.Text = x.Score.ToString();
                        rowIndex++;
                    }
                }

                //导出到文件
                string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
                string physicNewFile = Server.MapPath(newFile);
                doc.SaveAs(physicNewFile,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            }
            catch(Exception ex)
            {
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//关闭文档
                }
                if (app != null)
                {
                    app.Quit();//退出应用程序
                }
            }          
        }

 2、单元格合并的方式,即循环数据输出,然后对班级,人数,班主任列进行合并,但是合并的时候需要注意,假如某班级只有一个同学,也就是其实不需要合并的,

则必须排除在外,否则会出现”此命令无效“的异常,所以下面代码在合并的时候做了一个判断。

protected void CreateTableToExcel2()
        {
            Word.Application app = null;
            Word.Document doc = null;
            try
            {
                //构造数据
                List<Student> datas = new List<Student>();
                datas.Add(new Student { Leader = "小李", Name = "张三", Score = 498, StuClass = "一班" });
                datas.Add(new Student { Leader = "陈飞", Name = "李四", Score = 354, StuClass = "二班" });
                datas.Add(new Student { Leader = "陈飞", Name = "小红", Score = 502, StuClass = "二班" });
                datas.Add(new Student { Leader = "王林", Name = "丁爽", Score = 566, StuClass = "三班" });
                var cate = datas.GroupBy(s => s.StuClass);

                int rows = datas.Count + 1;
                int cols = 5;//表格列数
                object oMissing = System.Reflection.Missing.Value;
                app = new Word.Application();//创建word应用程序
                doc = app.Documents.Add();//添加一个word文档

                //输出大标题加粗加大字号水平居中
                app.Selection.Font.Bold = 700;
                app.Selection.Font.Size = 16;
                app.Selection.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                app.Selection.Text = "班级成绩统计单";

                //换行添加表格
                object line = Word.WdUnits.wdLine;
                app.Selection.MoveDown(ref line, oMissing, oMissing);
                app.Selection.TypeParagraph();//换行
                Word.Range range = app.Selection.Range;
                Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);

                //设置表格的字体大小粗细
                table.Range.Font.Size = 10;
                table.Range.Font.Bold = 0;

                //设置表格标题
                int rowIndex = 1;
                table.Cell(rowIndex, 1).Range.Text = "班级";
                table.Cell(rowIndex, 2).Range.Text = "姓名";
                table.Cell(rowIndex, 3).Range.Text = "成绩";
                table.Cell(rowIndex, 4).Range.Text = "人数";
                table.Cell(rowIndex, 5).Range.Text = "班主任";

                //循环数据创建数据行
                rowIndex++;
                foreach (var i in cate)
                {
                    int moveCount = i.Count() - 1;//纵向合并行数
                    if (moveCount.ToString() != "0")
                    {
                        table.Cell(rowIndex, 1).Merge(table.Cell(rowIndex + moveCount, 1));//合并班级
                        table.Cell(rowIndex, 4).Merge(table.Cell(rowIndex + moveCount, 4));//合并人数
                        table.Cell(rowIndex, 5).Merge(table.Cell(rowIndex + moveCount, 5));//合并班主任
                    }
                    //写入合并的数据并垂直居中
                    table.Cell(rowIndex, 1).Range.Text = i.Key;
                    table.Cell(rowIndex, 4).Range.Text = i.Count().ToString();
                    table.Cell(rowIndex, 5).Range.Text = i.First().Leader;
                    table.Cell(rowIndex, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 4).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 5).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    //构建姓名,成绩数据
                    foreach (var x in i)
                    {
                        table.Cell(rowIndex, 2).Range.Text = x.Name;
                        table.Cell(rowIndex, 3).Range.Text = x.Score.ToString();
                        rowIndex++;
                    }
                }
                //导出到文件
                string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
                string physicNewFile = Server.MapPath(newFile);
                doc.SaveAs(physicNewFile,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//关闭文档
                }
                if (app != null)
                {
                    app.Quit();//退出应用程序
                }
            }
        }

 

 

 

posted @ 2013-06-16 14:51  自由小菜园  阅读(21680)  评论(2编辑  收藏  举报