C#word模板添加多个表格,添加图片程序案例

来源:互联网 发布:房产经纪人网络端口 编辑:程序博客网 时间:2024/05/29 00:33
  public string CreateWordFile(string CheckedInfo)
        {
            string message = "";
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                Directory.CreateDirectory("F:/CNSI");  //创建文件所在目录
                string name = "CNSI_DATA.doc";
                object filename = "F://CNSI//" + name;  //文件保存路径
                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                object missing = System.Reflection.Missing.Value;
                object templatefile = CheckedInfo;
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing);
                WordDoc.Activate();


                //添加页眉
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
                WordApp.Selection.ParagraphFormat.LineSpacing = 10f;//设置文档的行间距


                //移动焦点并换行
                object count = 5;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                Microsoft.Office.Interop.Word.Table newTableTitle = WordDoc.Tables.Add(WordApp.Selection.Range, 2, 3, ref Nothing, ref Nothing);
                //设置表格样式
                newTableTitle.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                newTableTitle.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                //newTableTitle.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                //newTableTitle.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTableTitle.Columns[1].Width = 150f;
                newTableTitle.Columns[2].Width = 130f;
                newTableTitle.Columns[3].Width = 150f;


                newTableTitle.Cell(1, 1).Select();
                newTableTitle.Cell(1, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                //object moveCount = 5;
                object moveCount = 1;
                object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                //WordApp.Selection.Cells.Merge();
                //插入图片
                string FileName = @"F:\leo\project\pic1.jpg";//图片所在路径
                object LinkToFile = false;
                object SaveWithDocument = true;
                object Anchor = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                //WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 130f;//图片宽度
                //WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 90f;//图片高度


                newTableTitle.Cell(1, 3).Select();
                newTableTitle.Cell(1, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                object moveUnit1 = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object moveCount1 = 1;
                object moveExtend1 = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit1, ref moveCount1, ref moveExtend1);
                //插入图片
                string FileName1 = @"F:\leo\project\pic2.jpg";//图片所在路径
                object LinkToFile1 = false;
                object SaveWithDocument1 = true;
                object Anchor1 = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName1, ref LinkToFile1, ref SaveWithDocument1, ref Anchor1);


                newTableTitle.Cell(1, 2).Range.Text = "RTM圆桌会议申请表";
                newTableTitle.Cell(1, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                newTableTitle.Cell(1, 2).Range.Font.Size = 16;
                newTableTitle.Cell(1, 2).Range.Bold = 2;
                newTableTitle.Cell(1, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;


                object ncount = 2;
                WordApp.Selection.MoveDown(ref WdLine, ref ncount, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落


                Microsoft.Office.Interop.Word.Table newTableHead = WordDoc.Tables.Add(WordApp.Selection.Range, 2, 4, ref Nothing, ref Nothing);
                //设置表格样式
                newTableHead.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                newTableHead.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                //newTableHead.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                //newTableHead.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;


                newTableHead.Cell(1, 1).Range.Text = "EventNo:";
                newTableHead.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                newTableHead.Cell(1, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(1, 2).Range.Text = "RTM17000016";
                newTableHead.Cell(1, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(2, 1).Range.Text = "  Venue:";
                newTableHead.Cell(2, 1).Range.Bold = 2;//设置单元格中字体为粗体
                newTableHead.Cell(2, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(2, 2).Range.Text = "陕西省西安市西京医院陕西省西安市西京医院陕西省西安市西京医院陕西省西安市西京医院";
                newTableHead.Cell(2, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(2, 3).Range.Text = "Meeting Date:";
                newTableHead.Cell(2, 3).Range.Bold = 2;//设置单元格中字体为粗体
                newTableHead.Cell(2, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                newTableHead.Cell(2, 4).Range.Text = "2017-05-26 06:09:09";
                newTableHead.Cell(2, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                object tcount = 2;//移动行数
                WordApp.Selection.MoveDown(ref WdLine, ref tcount, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落


                //获取数据源
                DataTable dt = GetDataInfo();--此处需要方法实现数据源,具体列名参照下面表格处理;


                //文档中创建表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, dt.Rows.Count, 5, ref Nothing, ref Nothing);
                //设置表格样式
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;


                //填充表格内容
                newTable.Cell(1, 1).Range.Text = "Name";
                newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中


                //填充表格内容
                newTable.Cell(1, 2).Range.Text = "Title";
                newTable.Cell(1, 2).Range.Bold = 2;//设置单元格中字体为粗体
                newTable.Cell(1, 2).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


                //填充表格内容
                newTable.Cell(1, 3).Range.Text = "Hospitals&Speciaty/Organ";
                newTable.Cell(1, 3).Range.Bold = 2;//设置单元格中字体为粗体
                newTable.Cell(1, 3).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //纵向合并单元格
                //newTable.Cell(3, 3).Select();//选中一行
                newTable.Cell(1, 4).Range.Text = "Signature";
                newTable.Cell(1, 4).Range.Bold = 2;//设置单元格中字体为粗体
                newTable.Cell(1, 4).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


                newTable.Cell(1, 5).Range.Text = "Remarks";
                newTable.Cell(1, 5).Range.Bold = 2;//设置单元格中字体为粗体
                newTable.Cell(1, 5).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 5).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    newTable.Cell(2 + i, 1).Range.Text = dt.Rows[i]["Name"].ToString();
                    newTable.Cell(2 + i, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 1).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 2).Range.Text = dt.Rows[i]["Title"].ToString();
                    newTable.Cell(2 + i, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 2).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 3).Range.Text = dt.Rows[i]["Hospitals"].ToString(); 
                    newTable.Cell(2 + i, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 3).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 3).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 4).Range.Text = dt.Rows[i]["Signature"].ToString();
                    newTable.Cell(2 + i, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 4).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 4).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 5).Range.Text = dt.Rows[i]["Remarks"].ToString();
                    newTable.Cell(2 + i, 5).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 5).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 5).Range.Font.Size = 10;
                }


                //newTable.Cell(2, 1).Range.Text = "丁肇中";
                //newTable.Cell(2, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 1).Range.Font.Size = 10;
                //newTable.Cell(2, 2).Range.Text = "主治医师";
                //newTable.Cell(2, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 2).Range.Font.Size = 10;
                //newTable.Cell(2, 3).Range.Text = "陕西省西安市西京医院";
                //newTable.Cell(2, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 3).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 3).Range.Font.Size = 10;
                //newTable.Cell(2, 4).Range.Text = "丁肇中";
                //newTable.Cell(2, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 4).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 4).Range.Font.Size = 10;
                //newTable.Cell(2, 5).Range.Text = "备注";
                //newTable.Cell(2, 5).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 5).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 5).Range.Font.Size = 10;


                //在表格中增加行
                //WordDoc.Content.Tables[1].Rows.Add(ref Nothing);


                //文件保存
                WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                //if(newTableTitle.Rows.Count > 1)
                //{
                //    newTableTitle.Rows[newTableTitle.Rows.Count ].Delete();
                //}
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                message = name + "文档生成成功,以保存到C:CNSI下";
            }
            catch(Exception ex)
            {
                message = "文件导出异常!";
            }
            return message;

        }


     -------特殊讲解:

               object tcount = 2;//移动行数
                WordApp.Selection.MoveDown(ref WdLine, ref tcount, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落

截取的以上三行代码,MoveDown开始我也认知不是很清楚,其实就是移动当前焦点到下个模块,移动之后,需要WordApp.Selection.TypeParagraph();插入段落,

就可以重新开始插入表格,图片,《 object tcount = 2;//移动行数》重要参数,移动位置的参数,假设你上面已经录入两行,那么,它的值就可以设置成2,下一个模块内容

就和上面无缝衔接,不然会产生内容之间有较大的空格,甚至整个布局会混乱,尤为注意,互相学习,多多提意见;

阅读全文
1 0
原创粉丝点击