在C#中创建word文档
在下面文档中 首先引用word组件:Microsoft.Office.Interop.Word
在头文件中写上 using Word = Microsoft.Office.Interop.Word;
这样避免在文档中每一个地方都要写上一堆的Microsoft.Office.Interop.Word. 这样用一个 Word就能够代替都文件 简单明了
public string CreateWordFile(string CheckedInfo)
{
string message = "";
try
{
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:/abc"); //创建文件所在目录
string name = "CNSI_" + DateTime.Now.ToShortDateString()+".doc";
object filename = "C://CNSI//" + name; //文件保存路径
//创建Word文档
Word.Application WordApp = new Word.Application();
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 = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置 WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距 //移动焦点并换行
object count = ;
object WdLine = Word.WdUnits.wdLine;//换一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
WordApp.Selection.TypeParagraph();//插入段落 //文档中创建表格
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, , , ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = 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 = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 //填充表格内容
newTable.Cell(, ).Range.Text = "产品基本信息";
newTable.Cell(, ).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
//合并单元格
newTable.Cell(, ).Merge(newTable.Cell(, ));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; //填充表格内容
newTable.Cell(, ).Range.Text = "品牌名称:";
newTable.Cell(, ).Range.Text = "BrandNameBrandName";
//纵向合并单元格
newTable.Cell(, ).Select();//选中一行
object moveUnit = Word.WdUnits.wdLine;
object moveCount = ;
object moveExtend = Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//插入图片
string FileName = @"C:\Users\qigang\Desktop\题库\第 4 章 答案 标准\4 答案 填空题0.8_04.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;//图片高度
//将图片设置为四周环绕型
Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[].ConvertToShape();
s.WrapFormat.Type = 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 = 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
{
message = "文件导出异常!";
}
return message;
}
在C#中创建word文档的更多相关文章
- 如何在程序中给word文档加上标和下标
如何在程序中给word文档加上标和下标 上标或下标是一个小于普通行格式的数字,图形,标志或者指示通常它的设置与行相比偏上或偏下.下标通常显示于或者低于基准线,而上标则高于.上标和下标通常被用于表达公式 ...
- C# 中使用Word文档对图像进行操作
C# 中使用Word文档对图像进行操作 Download Files: ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操 ...
- ABBYY PDF Transformer+从文件选项中创建PDF文档的教程
可使用OCR文字识别软件ABBYY PDF Transformer+从Microsoft Word.Microsoft Excel.Microsoft PowerPoint.HTML.RTF.Micr ...
- C#实现通过模板自动创建Word文档的方法
原文地址:http://www.jb51.net/article/55332.htm 本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧.分享给大家供大家参考.具体实现方法 ...
- [java,2017-05-04] 创建word文档
package test; import java.text.SimpleDateFormat; import java.util.Date; import com.aspose.words.Data ...
- Java 后台创建word 文档
---恢复内容开始--- Java 后台创建 word 文档 自己总结 网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...
- 在Delphi中处理word文档与数据库的互联 1
在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...
- 在Delphi中处理word文档与数据库的互联
在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...
- OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚
using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(@"C:\********.doc ...
随机推荐
- Knockout.Js官网学习(简介)
前言 最近一段时间在网上经常看到关于Knockout.js文章,于是自己就到官网看了下,不过是英文的,自己果断搞不来,借用google翻译了一下.然后刚刚发现在建立asp.net mvc4.0的应用程 ...
- CSS计数器与动态计数呈现
代码: CSS代码: body { counter-reset: icecream; } input:checked { counter-increment: icecream; } .total:: ...
- linux 常用命令及技巧
linux 常用命令及技巧 linux 常用命令及技巧:linux 常用命令总结: 一. 通用命令: 1. date :print or set the system date and time 2. ...
- mysql实体关系(mysql学习五)
实体关系 表设计 1:1 两个实体表内,存在相同的主键字段 如果记录的主键值等于另一个关系表内记录的主键值,则两条记录的对应为一一对应 优化上称为垂直分割 1:n 一个实体对应多个其他实体(一个班级 ...
- php下使用phpmailer发送邮件
由于默认虚拟空间不支持mail()函数,客户需要留言发送邮件,找到phpmailer发送不成功,调试成功后记录一下. 最新的下载地址在github,https://github.com/Synchro ...
- Ubuntu14.04 切换root账户su root失败解决办法
原因是需要备份一个vimrc,可是cp就提示Permission denied. su root就提示su: Authentication failure 解决办法: sudo passwd root ...
- cxgrid对经过筛选过的数据的选择(反选)
// 下面这个主要是对查询出来的数据, 经过筛选后得到的数据中进行反选操作 ,然后对选择的数据进行修改(全选或选择一部分也可以根据些代码修改) Screen.Cursor := crHourGlass ...
- m3u8
audo apt-get install pkg-configsudo apt-get install automake autoconf m4 libtool sudo apt-get instal ...
- Html5 Canvas一个简单的画笔例子
相比了下Qt quick的canvas和HTML5的canvas,发现HTML5 Canvas在同样绘制绘制操作下性能比Qt的canvas强很多,附上一个HTML5 canvas画笔一例子 var D ...
- C基础 那些年用过的奇巧淫技
引言 - 为寻一颗明星 为要寻一颗明星 徐志摩 1924年12月1日<晨报六周年纪念增刊> 我骑著一匹拐腿的瞎马, 向著黑夜里加鞭:—— 向著黑夜里加鞭, 我跨著一匹拐腿的瞎马.// 我冲 ...