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. 前端自动化部署之gulp

    1.首先需要安装node+npm(这里不再叙述,网上教程一大堆) 2.gulp全局安装:npm install -g gulp 3.cd进入到你的项目目录,这里使用demo文件夹为我的示例项目 4.在 ...

  2. javascript实现验证身份证号的有效性并提示

    javascript实现验证身份证号的有效性并提示 function nunber(allowancePersonValue){ if(allowancePersonValue=="身份证号 ...

  3. Hibernate HQL查询语句总结

    Hibernate HQL查询语句总结 1. 实体查询:有关实体查询技术,其实我们在先前已经有多次涉及,比如下面的例子:String hql="from User user ";L ...

  4. react学习笔记-02

    1.组件嵌套 React允许将代码封装成一个component,然后像html标签一样,插入网页中中. var HelloMessage = React.createClass({ render: f ...

  5. Boolean对象 识记

    Boolean 对象表示两个值:"true" 或 "false". 1.创建 new Boolean(value); //构造函数 返回 对象+返回值 Bool ...

  6. 快速排序(js版本)

    快速排序的时间复杂度为:O(n*log2n),相比较其他O(n2)的排序算法,还是比较有优势的.原文参考在此处,因为本人对原文的一小段代码有点不理解,所以进行了小的修改. 1.基本思想:在数组的第一个 ...

  7. linux的用户,群组和其他用户

    1 linux 安全模型:(多任务,多用户的操作系统) 1)使用user和group控制使用者对文件的存储权限. 2)用户使用账户和口令登录linux 3)每个文件都有owner(创建者),owner ...

  8. iOS7之后的文本高度封装

    #import "NSString+Util.h" @implementation NSString (Util) +(CGFloat)changeStationWidth:(NS ...

  9. Error js内置错误 js处理错误流程 Throw语句

    Exceptional Exception Handling in JavaScript       MDN资料 Anything that can go wrong, will go wrong. ...

  10. Java. Warning – Build path specifies execution environment J2SE-1.5

    Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace th ...