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. ie Css Hack 特殊符号

    Css Hack 特殊符号 (1)* :IE6/7都能识别*,标准浏览器不识别(2)_:只有IE6识别(3)!Important:IE6不识别,Firefox,IE7/8/9.chorme等主流浏览器 ...

  2. Android UI方面的学习记录

    1,android:textAllCaps=“false” android5.0后有可能button的text显示全是大写,设置这个后才能正常显示小写 2,优化listview性能: 1,view重用 ...

  3. 解决gstreamer无法播放的bug

    0 ls 1 ./start.sh 2 ps 3 kill 366 4 cd /usr/app/services/ 5 ./start.sh 6 cd ../ 7 ls 8 cd res/ 9 ls ...

  4. maven 3.3.9-bin 和 maven 3.3.9-src 的区别 以及 maven安装包的 .tar.gz后缀与.zip 后缀的区别

    (maven 3.3.9-bin)一个是class的文件包,由java文件编译成的,(maven 3.3.9-src )一个是java文件包即是源码(.tar.gz后缀)是linux的压缩包,(.zi ...

  5. 如何取消input记忆功能

    默认情况下,input会有这个记忆功能,如果不想让它记忆,可以在input上加上autocomplete="off"即可.

  6. CodeForces 566D 并查集集合合并

    #include <stdio.h> #include <algorithm> #define MAX 100000 #define LL long long #define ...

  7. Provably Delay Efficient Data Retrieving in Storage Clouds---INFOCOM 2015

    [标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption future work [相关方法或论文] [重点提示] [其它]

  8. 数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份

    结尾日志的问题 还原选择中去掉结尾日志就可以了 转载自:http://blog.csdn.net/aojiancc2/article/details/46316451

  9. OAuth2.0 工作流程

    重要术语   Authorization Server:授权服务器,能够成功验证资源拥有者和获取授权,并在此之后分发令牌的服务器: Resource Server:资源服务器,存储用户的数据资源,能够 ...

  10. php 5.5 xhprof for windows

    今天看到一个好的性能优软件xhprof(有facebook开发的类库)在国内找了很多网站都没有找到相关php5.5的扩展,只好FQ还是外面的世界精彩一下就找到了link (http://windows ...