在下面文档中  首先引用word组件:Microsoft.Office.Interop.Word

在头文件中写上 using Word = Microsoft.Office.Interop.Word;

这样避免在文档中每一个地方都要写上一堆的Microsoft.Office.Interop.Word.  这样用一个 Word就能够代替都文件  简单明了

  1. public string CreateWordFile(string CheckedInfo)
  2. {
  3. string message = "";
  4. try
  5. {
  6. Object Nothing = System.Reflection.Missing.Value;
  7. Directory.CreateDirectory("C:/abc"); //创建文件所在目录
  8. string name = "CNSI_" + DateTime.Now.ToShortDateString()+".doc";
  9. object filename = "C://CNSI//" + name; //文件保存路径
  10. //创建Word文档
  11. Word.Application WordApp = new Word.Application();
  12. 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 = 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 = Word.WdUnits.wdLine;//换一行;
  26. WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
  27. WordApp.Selection.TypeParagraph();//插入段落
  28.  
  29. //文档中创建表格
  30. Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, , , ref Nothing, ref Nothing);
  31. //设置表格样式
  32. newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
  33. newTable.Borders.InsideLineStyle = 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 = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
  44. WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
  45.  
  46. //填充表格内容
  47. newTable.Cell(, ).Range.Text = "产品基本信息";
  48. newTable.Cell(, ).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
  49. //合并单元格
  50. newTable.Cell(, ).Merge(newTable.Cell(, ));
  51. WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  52.  
  53. //填充表格内容
  54. newTable.Cell(, ).Range.Text = "品牌名称:";
  55. newTable.Cell(, ).Range.Text = "BrandNameBrandName";
  56. //纵向合并单元格
  57. newTable.Cell(, ).Select();//选中一行
  58. object moveUnit = Word.WdUnits.wdLine;
  59. object moveCount = ;
  60. object moveExtend = Word.WdMovementType.wdExtend;
  61. WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
  62. WordApp.Selection.Cells.Merge();
  63. //插入图片
  64. string FileName = @"C:\Users\qigang\Desktop\题库\第 4 章 答案 标准\4 答案 填空题0.8_04.jpg";//图片所在路径
  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. Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[].ConvertToShape();
  73. s.WrapFormat.Type = 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 = 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. }

在C#中创建word文档的更多相关文章

  1. 如何在程序中给word文档加上标和下标

    如何在程序中给word文档加上标和下标 上标或下标是一个小于普通行格式的数字,图形,标志或者指示通常它的设置与行相比偏上或偏下.下标通常显示于或者低于基准线,而上标则高于.上标和下标通常被用于表达公式 ...

  2. C# 中使用Word文档对图像进行操作

    C# 中使用Word文档对图像进行操作 Download Files: ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操 ...

  3. ABBYY PDF Transformer+从文件选项中创建PDF文档的教程

    可使用OCR文字识别软件ABBYY PDF Transformer+从Microsoft Word.Microsoft Excel.Microsoft PowerPoint.HTML.RTF.Micr ...

  4. C#实现通过模板自动创建Word文档的方法

    原文地址:http://www.jb51.net/article/55332.htm   本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧.分享给大家供大家参考.具体实现方法 ...

  5. [java,2017-05-04] 创建word文档

    package test; import java.text.SimpleDateFormat; import java.util.Date; import com.aspose.words.Data ...

  6. Java 后台创建word 文档

    ---恢复内容开始--- Java 后台创建 word 文档 自己总结  网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...

  7. 在Delphi中处理word文档与数据库的互联 1

    在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...

  8. 在Delphi中处理word文档与数据库的互联

    在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...

  9. OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚

    using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(@"C:\********.doc ...

随机推荐

  1. 使用nodejs引用socket.io做聊天室

    Server: var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs ...

  2. 利用脚本设置本机IP地址

    各位同学,在日常工作中.常出现需要指定IP的地址的清况.为了解决这一个问题,我特意为自己编写了一段脚本.方便设定自己笔记本的IP地址.供大家参考. 其中包括无线wifi和有线网络设定两个IP的操作. ...

  3. Zookeeper 脑裂

    转自 http://blog.csdn.net/u010185262/article/details/49910301 Zookeeper zookeeper是一个分布式应用程序的协调服务.它是一个为 ...

  4. 正确使用stl map的erase方法

    先声明:下面的文章是针对windows的用法,因为std::map的erase函数的windows的实现版本是返回一个std::map的迭代器,但是STL标准里面的该函数的返回值确是: map.era ...

  5. delphi 2010是动画GIF的支持方法

    下面delphi 2010是动画GIF的支持方法:  1.在窗体上放一个Image1控件.注意:这时设置其Picture属性,加载一幅动画GIF是不会动画显示的.  2.在窗体的FormCreate事 ...

  6. 奇怪的函数 (codevs 3538/1696) 题解

    [题目描述] 给定n,使得x^x达到或超过n位数字的最小正整数x是多少? [样例输入] 11 [样例输出] 10 [解题思路] 首先想到枚举,但是范围有点大,n<=2*10^9,果断用二分.其实 ...

  7. 实战Django:官方实例Part2

    我们接着Part1部分往下讲.我们在part1中启动服务器后,并没有在管理页面中发现新添加的Polls应用,怎么办捏? 7.在管理界面中显示Question 只要注册一下这个应用就可以了.编辑poll ...

  8. Cygwin ssh服务配置 (SecureCRT连接Cygwin配置)

    1.运行ssh-host-config 这里需要注意的是标红部分,输入的用户名或密码要符合计算机的用户名或密码策略(尤其是公司有权限限制的电脑). $ ssh-host-config *** Quer ...

  9. Android SharedPreferences使用以及原理详解

    SharedPreferences的使用非常简单,能够轻松的存放数据和读取数据.SharedPreferences只能保存简单类型的数据,例如,String.int等.一般会将复杂类型的数据转换成Ba ...

  10. AsyncTask和Handler两种异步方式的实现和区别比较

    1  AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以 ...