using Microsoft.Office.Interop.Word;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;

 namespace WindowsFormsApplication2
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }

         public string CreateWordFile(string CheckedInfo)
         {
             string message = "";
             try
             {
                 Object Nothing = System.Reflection.Missing.Value;
                 Directory.CreateDirectory("C:/CNSI");  //创建文件所在目录
                 string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc";
                 object filename = "C://CNSI//" + name;  //文件保存路径
                 //创建Word文档
                 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                 Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                 //添加页眉
                 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 = 15f;//设置文档的行间距

                 //移动焦点并换行
                 ;
                 object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                 WordApp.Selection.TypeParagraph();//插入段落

                 //文档中创建表格
                 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, , , ref Nothing, ref Nothing);
                 //设置表格样式
                 newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                 newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                 newTable.Columns[].Width = 100f;
                 newTable.Columns[].Width = 220f;
                 newTable.Columns[].Width = 105f;

                 //填充表格内容
                 newTable.Cell(, ).Range.Text = "产品详细信息表";
                 newTable.Cell(, ).Range.Bold = ;//设置单元格中字体为粗体
                 //合并单元格
                 newTable.Cell(, ).Merge(newTable.Cell(, ));
                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

                 //填充表格内容
                 newTable.Cell(, ).Range.Text = "产品基本信息";
                 newTable.Cell(, ).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
                 //合并单元格
                 newTable.Cell(, ).Merge(newTable.Cell(, ));
                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                 //填充表格内容
                 newTable.Cell(, ).Range.Text = "品牌名称:";
                 newTable.Cell(, ).Range.Text = "BrandName";
                 //纵向合并单元格
                 newTable.Cell(, ).Select();//选中一行
                 object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                 ;
                 object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                 WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                 WordApp.Selection.Cells.Merge();
                 //插入图片
                 string FileName = @"c:\picture.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[].Width = 100f;//图片宽度
                 WordDoc.Application.ActiveDocument.InlineShapes[].Height = 100f;//图片高度
                 //将图片设置为四周环绕型
                 Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[].ConvertToShape();
                 s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

                 newTable.Cell(, ).Range.Text = "产品特殊属性";
                 newTable.Cell(, ).Merge(newTable.Cell(, ));
                 //在表格中增加行
                 WordDoc.Content.Tables[].Rows.Add(ref Nothing);

                 WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                 WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                 //文件保存
                 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);
                 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                 message = name + "文档生成成功,以保存到C:CNSI下";
             }
             catch(Exception e)
             {
                 throw e;
                 message = "文件导出异常!";
             }
             return message;
         }

         private void button1_Click_1(object sender, EventArgs e)
         {
             CreateWordFile("c:\\dd.doc");
         }
     }
 }

注意 :需要引用 COM组件Microsoft.Office.Interop.Word

如果编译出现"无法嵌入互操作类型 请改用适用的接口"错误 右键上述组件 点击属性  嵌入互操作类型改为false

C#生成word的更多相关文章

  1. Aspose.Words简单生成word文档

    Aspose.Words简单生成word文档 Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.Documen ...

  2. php 生成word的三种方式

    原文地址 http://www.jb51.net/article/97253.htm 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像只要是标题带PHP的貌似点击 ...

  3. 代码批量生成WORD的遇到的问题及解决

    好久没搞工具了,最近因为处理大规模公文处理单文档,自己写了个批量处理WORD的程序:在调试过程中,主要遇到两个问题 第一个是WORD的模板 数据很多,但是WORD模板只需要一个,将数据替换WORD里标 ...

  4. ASP.NET生成WORD文档,服务器部署注意事项

    网上转的,留查备用,我服务器装的office2007所以修改的是Microsoft Office word97 - 2003 文档这一个. ASP.NET生成WORD文档服务器部署注意事项 1.Asp ...

  5. [转载]Matlab生成Word报告

    最近在进行一批来料的检验测试,一个个手动填写报告存图片太慢了,就有了种想要使用Matlab在分析完后数据可以自动生成PDF报告的想法,于是就去网上搜索了相关的资料,发现Matlab中文论坛上有xiez ...

  6. POI生成WORD文档

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  7. poi生成word文件

    一.简介 对于poi来说,poi可以完成对word.excel.ppt的处理.word目前有两种文件格式,一种是doc后缀.另一种是docx后缀的.2007之前的版本都是doc后缀的,这种格式poi使 ...

  8. 根据指定Word模板生成Word文件

    最近业务需要批量打印准考证信息 1.根据Table数据进行循环替换,每次替换的时候只替换Word中第一个Table的数据, 2.每次替换之后将Word中第一个Table数据进行复制,将复制Table和 ...

  9. 使用freemarker生成word,步骤详解并奉上源代码

    1.   步骤 1.    用word编辑好模板 1. 普通字符串替换为 ${string} 2. 表格循环用标签 <#list userList as user> 姓名:${user.u ...

  10. PHP生成word的三种方式

    摘要: 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像在博客园发表博客只要是标题带PHP的貌似点击量都不是很高(哥哥我标题还是带上PHP了),不知道为什么,估计 ...

随机推荐

  1. ios 显示其他app的购买页面

    using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...

  2. iOS编程中throttle那些事

    不知道大家对throttle这个单词是否看着眼熟,还是说对这个计算机基础概念有很清晰的了解了.今天就来聊聊和throttle相关的一些技术场景. 定义 我经常有一种感觉,对于英语这门语言的语感,会影响 ...

  3. mysql中文显示问号

    mysql插入中文后显示为?,查到http://blog.csdn.net/chenxingzhen001/article/details/7567812中方法,即 在my.ini配置文件中的[myd ...

  4. 【转】HDU1028

    转自博客园ID:2108,老卢同志 http://www.cnblogs.com/--ZHIYUAN/p/6102893.html Ignatius and the Princess III Time ...

  5. 强制改变IE中的文本模式

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  6. php 函数积累第一天

    htmlspecialchars 函数格式化表单输入的转化为html形式 exit - 输出一个消息并且退出当前脚本 list() 用一步操作给一组变量进行赋值. <?php$info = ar ...

  7. thinkphp 匹配qq 表情

    <?php// 匹配qq表情    function replace_phiz($content){        preg_match_all('/\[.*?\]/is',$content,$ ...

  8. html readonly和disabled的区别

    今天我创建了一个html表单,其中一个字段是sequence number, 这个字段是用ajax异步从后台获取自动生成的,我不希望用户修改这个值,于是我使用了如下html代码来处理: <for ...

  9. DELPHI中MessageBox的用法 (转)

    MessageBox对话框 输入控件的   ImeName属性把输入法去掉就默认为英文输入了 MessageBox对话框是比较常用的一个信息对话框,其不仅能够定义显示的信息内容.信息提示图标,而且可以 ...

  10. digitalocean优惠码30美元1G内存VPS免费使用两个月

    著名的云主机服务商CloudWays送福利啦!CloudWays是一家云主机网站服务商,与Digitalocean和亚马逊AWS开展合作,新用户注册验证手机即可赠送价值30美元的免费VPS主机. 申请 ...