1. public string CreateWordFile(string CheckedInfo)
  2.         {
  3.             string message = "";
  4.             try
  5.             {
  6.                 Object Nothing = System.Reflection.Missing.Value;
  7.                 Directory.CreateDirectory("C:/CNSI");  //创建文件所在目录
  8.                 string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc";
  9.                 object filename = "C://CNSI//" + name;  //文件保存路径
  10.                 //创建Word文档
  11.                 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
  12.                 Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
  13.  
  14.                 //添加页眉
  15.                 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
  16.                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
  17.                 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
  18.                 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
  19.                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
  20.  
  21.                 WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
  22.  
  23.                 //移动焦点并换行
  24.                 object count = ;
  25.                 object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
  26.                 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
  27.                 WordApp.Selection.TypeParagraph();//插入段落
  28.  
  29.                 //文档中创建表格
  30.                 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, , , ref Nothing, ref Nothing);
  31.                 //设置表格样式
  32.                 newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
  33.                 newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
  34.                 newTable.Columns[].Width = 100f;
  35.                 newTable.Columns[].Width = 220f;
  36.                 newTable.Columns[].Width = 105f;
  37.  
  38.                 //填充表格内容
  39.                 newTable.Cell(, ).Range.Text = "产品详细信息表";
  40.                 newTable.Cell(, ).Range.Bold = ;//设置单元格中字体为粗体
  41.                 //合并单元格
  42.                 newTable.Cell(, ).Merge(newTable.Cell(, ));
  43.                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
  44.                 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
  45.  
  46.                 //填充表格内容
  47.                 newTable.Cell(, ).Range.Text = "产品基本信息";
  48.                 newTable.Cell(, ).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
  49.                 //合并单元格
  50.                 newTable.Cell(, ).Merge(newTable.Cell(, ));
  51.                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  52.  
  53.                 //填充表格内容
  54.                 newTable.Cell(, ).Range.Text = "品牌名称:";
  55.                 newTable.Cell(, ).Range.Text = "BrandName";
  56.                 //纵向合并单元格
  57.                 newTable.Cell(, ).Select();//选中一行
  58.                 object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
  59.                 object moveCount = ;
  60.                 object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
  61.                 WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
  62.                 WordApp.Selection.Cells.Merge();
  63.                 //插入图片
  64.                 string FileName = @"J:/C#/WebApplication1/images/login_3(3).gif";//图片所在路径
  65.                 object LinkToFile = false;
  66.                 object SaveWithDocument = true;
  67.                 object Anchor = WordDoc.Application.Selection.Range;
  68.                 WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
  69.                 WordDoc.Application.ActiveDocument.InlineShapes[].Width = 100f;//图片宽度
  70.                 WordDoc.Application.ActiveDocument.InlineShapes[].Height = 100f;//图片高度
  71.                 //将图片设置为四周环绕型
  72.                 Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[].ConvertToShape();
  73.                 s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
  74.  
  75.                 newTable.Cell(, ).Range.Text = "产品特殊属性";
  76.                 newTable.Cell(, ).Merge(newTable.Cell(, ));
  77.                 //在表格中增加行
  78.                 WordDoc.Content.Tables[].Rows.Add(ref Nothing);
  79.  
  80.                 WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
  81.                 WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
  82.  
  83.                 //文件保存
  84.                 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);
  85.                 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
  86.                 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  87.                 message = name + "文档生成成功,以保存到C:CNSI下";
  88.             }
  89.             catch
  90.             {
  91.                 message = "文件导出异常!";
  92.             }
  93.             return message;
  94.         }

以上内容转自   https://blog.csdn.net/lj102800/article/details/6106451

C# 在Word表格中插入新行(表格含合并行)的更多相关文章

  1. INSERT INTO 语句用于向表格中插入新的行。

    语法 INSERT INTO 表名称 VALUES (值1, 值2,....) 我们也可以指定所要插入数据的列: INSERT INTO table_name (列1, 列2,...) VALUES ...

  2. 如何使用poi在word表格中插入行的4种方法

    本文记录了,在word表格中插入新行的几种方法.直接上代码说明 table.addNewRowBetween 没实现,官网文档也说明,只有函数名,但没具体实现,但很多文章还介绍如何使用这个函数,真是害 ...

  3. 在页面上绘制一张表格,使用 DOM 节点的动态添加和删除向表格中插入数据,点击表格每行后的“删除”超链接

    查看本章节 查看作业目录 需求说明: 在页面上绘制一张表格,使用 DOM 节点的动态添加和删除向表格中插入数据,点击表格每行后的"删除"超链接,使用 DOM 节点的删除操作将对应的 ...

  4. SQL语句 在一个表中插入新字段

    SQL语句 在一个表中插入新字段: alter table 表名 add 字段名 字段类型 例: alter table OpenCourses add Audio varchar(50)alter ...

  5. 关于HTML表格中插入背景图片的问题_百度知道 3个回答 - 提问时间: 2009年03月23日 最佳答案: <tr style="background-image:url(1.jpg)"> (这事设置背景图片) <img src="images/bbs_student1.gif" />如果是这样的就是直接插入图片。你看看,...

    关于HTML表格中插入背景图片的问题_百度知道 3个回答 - 提问时间: 2009年03月23日 最佳答案: <tr style="background-image:url(1.jpg ...

  6. [Swift通天遁地]二、表格表单-(3)在表格中嵌套另一个表格并使Cell的高度自适应

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. [word]2010中插入公式自动编号并且公式不自动缩小/变小

    要实现在word2010中插入公式自动编号,就要用到自动图文集功能,具体操作如下: 1.先制定制表位位置:单击一个空白段落,然后双击标尺线的底部:这会激活"制表位"对话框,如图所示 ...

  8. 从一个word文件中读取所有的表格和标题(1)

    首先讲需求: 从word文件中读表格里的数据,然后插入数据库中.word文件中的表格是带有标题的,把标题读出来,进行匹配数据库. 需求分析: word2007底层是以xml文件存储的,所以分析xml的 ...

  9. Bootstrap css栅格 + 网页中插入代码+css表格

    设计达人 http://www.shejidaren.com/30-minimal-app-icons.html CSS栅格: <!DOCTYPE html> <html lang= ...

随机推荐

  1. jquery自带的排序方法(js也是)

    jquery.sort()   js.sort() <!DOCTYPE html> <html>   <head>     <meta charset=&qu ...

  2. python3 内置函数enumerate

    一.简介: 该函数在字面上是枚举.列举的意思,用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列, 同时列出数据和数据下标,一般用在 for 循环当中,可同时得到数据对象的值及对应的 ...

  3. Java反射及注解学习- 反射的使用 - JDK动态代理

    代理模式基本概念:1.代理模式的作用:为其他对象提供一种以控制对方的访问在某种情况下,一个客户不想或者不能直接引用另一个对象,代理可以在客户端和目标对象之间起到中介的作用代理的角色:(1)抽象角色:声 ...

  4. Delphi读取和写入utf-8编码格式的文件

    读取UTF-8格式的文件内容 function LoadUTF8File(AFileName: string): string; var ffileStream:TFileStream; fAnsiB ...

  5. Linux二进制程序安装使用

    下载好的二进制,压缩包解压,或者直接是二进制. 放到想要的目录 在 /etc/environment 双引号前面添加程序路径 以:开头,\结尾可以换行 接下来修改sudo ,不然sudo会找不到 以下 ...

  6. wsl和windows相互访问文件夹

    How to access Windows folders from Bash on Ubuntu on Windows You'll find the Windows C:\ structure a ...

  7. printf ("%*.*s")

    小数点.后“*”表示输出位数,具体的数据来自参数表printf格式字符串中,与宽度控制和精度控制有关的常量都可以换成变量,方法就是使用一个“*”代替那个常量,然后在后面提供变量给“*”. 同样,小数点 ...

  8. windows汇编语言开发环境搭建

    1.下载软件mash32 http://www.masm32.com/download/masm32v11r.zip 此软件包含对汇编文件的编译和运行,下载后直接运行并解压即可 2.配置环境变量 在系 ...

  9. iview+vue 使用中遇到的问题(表格、select、radio)

    1.iview+vue中,对表头的动态设置: iview表头若是需要动态设置,可以有两个方法,第一种: children: [ { title: '2017年', align: 'center', k ...

  10. vue2.0 项目小总结

    最近做了一个vue的PC端的项目,不大,真正用到vue的东西也不是太多,逻辑处理用到了不少原生js东西. 1.图片渲染 后台返回base64格式数据,一开始绑定src,提示pic字段未定义,懵逼了好久 ...