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. 洛谷-统计数字-NOIP2007提高组复赛

    题目描述 Description 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照 ...

  2. Jquery中日期插件jquery.datepick的使用

    jsp的代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...

  3. BigDecimal-解决商业计算

    1.String to BigDecimal String amtStr = "1234.56"; BigDecimal amtBD = new BigDecimal(amtStr ...

  4. removeObjectAtIndex

    CGFloat lableW = (baseViewWidth - 2)/3;// dcj20150724,减2是为了解决字体模糊的问题,因为设置了边框. 原因是下面引起的 titleview.lay ...

  5. C++知识体系

    基础知识 推荐书目 C++ <C++程序设计>(课程教材即可,简而薄) <STL源码剖析>(对C++进一步深化,也是必备知识) <C++对象模型>(经典中经典,重点 ...

  6. JAVA类与对象(课堂总结)

    一:"=="的不同含义 当"=="施加于原始数据类型变量时,是比较变量所保存的数据是否相等当"=="施加于引用类型变量时,是比较这两个变量是 ...

  7. linux和windows双系统时间错误解决方法

    转自http://www.2cto.com/os/201204/126212.html windows时间会慢8小时,原因: 两个概念: UTC即Universal Time Coordinated, ...

  8. lldb 命令

    po [[UIWindow keyWindow] recursiveDescription]

  9. 顺序栈和链式栈(C++实现)

    顺序栈,是一种基于数组的存储表示. 实现类代码如下: template<class T> class SeqStack{ T *element; int top; int maxSize; ...

  10. Intent Flag实际项目 -- 超时跳转登录界面并清理前面所有activity

    项目中涉及到登录超时跳转登录界面的逻辑,我以前的跳转flag为Intent.FLAG_ACTIVITY_CLEAR_TOP,但是点击返回按钮还是会回到上个界面.代码如下: ActivityUtils. ...