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,下一个模块内容

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

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

  1. Word模板文件,替换内容 文本、图片、表格、列表、区块、嵌套等

    Word模板文件,替换内容 文本.图片.表格.列表.区块.嵌套等 实例:员工信息表Word文件,程序动态替换内容,生成pdf文件 话不多说,先看效果图! 优势: 1.模板只是布局调整,程序无需修改! ...

  2. java poi操作word2007_java使用poi解析2007以上的word文档中的表格与图片

    项目中使用到了要解析word文档中的表格与图片,网上的2003的解析方式很多,2007以上的很少,我看了官网找了资料自己写了一个简单的解析方案,大家共同学习吧!有不对的地方希望大神指教! import ...

  3. WPS(word)批量给所有表格添加题注(标题)

    我们在工作中经常会遇到需要给word中的所有表格添加 "表1-1" 这样的题注,如果是我们自己写的话,一开始我们就可以设置自动添加.但是如果这篇文章是别人已经写好的我们该怎么办呢? ...

  4. 【PHPWord】基于Word模板替换生成输出表格动态生成内容、合并单元格、设置单元格背景颜色

    文章目录 一.前言 二.业务需求 三.功能实现 1.处理数据 2.Word模板 3.输出动态数据 四.完整代码和模板 1.Word模板 2.完整代码 五.总结 一.前言 在之前的文章中,我们做了直接生 ...

  5. 如何在html表格中添加超链接,excel表格添加网页链接(网页 链接execl 制作 列表)...

    如何在一个excel表中做数据链接 在一个相对比较大表里点击一个数据的时候 选择你所说的那个数据--右键--超链接--在左侧选择第2项--然后选择表格名称--上面指定单元格--确定 怎么样把网页中的表 ...

  6. java实现将数据导出为word功能(文字,表格,图片的循环导出)

    1.配置文件的准备 1.导出功能实现所需要的pom文件 <!-- 导出到word(循环图片) --><!-- word导出 方式:easypoi--><dependenc ...

  7. 【C#】C#使用Microsoft.Office.Interop.Word操作Word文档,向表格插入图片

    上篇:[C#]C#使用Microsoft.Office.Interop.Word操作Word文档,替换文本 Word模板: 代码: try {Application app = new Applica ...

  8. POI 写word,添加标题,表格,图片,自动生成目录,合并单元格

    工程地址:https://github.com/zheng-chang-wei/word package com.example.demo1.poi;import org.apache.poi.xwp ...

  9. 利用Freemaker模板引擎制作包含表格和图片的word导出模板

    目录 1.设置好word文档内容及格式 2.将word文档另存为xml格式 3.$占位符替换 4.图片换成占位符 1.设置好word文档内容及格式 将需要动态输出的数据用${}来占位标记,图片就需要在 ...

最新文章

  1. 使用OpenCV,Python和dlib进行眨眼检测及计数
  2. 炸裂!微软重磅推出混合现实平台 Mesh、基于 Excel 的低代码语言 Power Fx,Ignite 2021 太精彩!...
  3. (0026)iOS 开发之模块化封装初步实践
  4. Nginx概述与安装
  5. springmvc的异步处理
  6. Andriod --- JetPack (三):ViewModel 的诞生
  7. 开源 java CMS - FreeCMS2.4 菜单管理
  8. 程序包org.springframework.stereotype不存在,解决办法
  9. 为IP v6划分子网
  10. python降维之时间类型数据的处理_使用Python进行数据降维|线性降维
  11. 90期:我在系统设计上犯过的14个错
  12. 7时过2小时是几时_小姐姐花1万7租了一个2小时男友,全程上演偶像剧......
  13. MySql中varchar(10)和varchar(100)的区别==以及char的利弊
  14. python解释器需要另外安装吗_Python解释器安装
  15. java 汉字转拼音_java实现汉字转拼音
  16. c++的vector初始化
  17. python识别手写文字_深度学习---手写字体识别程序分析(python)
  18. 免费域名邮箱如何申请?怎么给国外发邮件?
  19. 【附源码】Python计算机毕业设计七七美食汇
  20. 蜗牛星际A单开启WOL功能

热门文章

  1. excel每页小计累计_您还需要Excel小计吗?
  2. 不可用于python的标识符有_下面哪个不是Python合法的标识符
  3. gis中的加权求和工具在哪里_干货分享 | 地理加权回归介绍及其arcgis软件操作
  4. 【原创】RPA在保险行业的场景分享-中科云创CEO每日分享
  5. 你管这叫操作系统源码(九)
  6. Python数据可视化 Pyecharts 制作 Map 地图
  7. 拉线油门和电子油门的比较
  8. 电池容量工作时间如何计算
  9. 【操作系统】MIT 6.s081 LAB5
  10. STM32F1驱动AM2302温湿度传感器