下面一个函数,建立一个Word 文档,添加页眉、页脚,在内容中两个不同字体的Hello!!!  

来自 <http://bbs.csdn.net/topics/340041961>

public void myFunction()

{

Word.ApplicationClass oWordApp = new Word.ApplicationClass();

//建立Word 对象,启动word程序

object missing = System.Reflection.Missing.Value;

object oTemplate = System.Windows.Forms.Application.StartupPath+"\\mytemplate.dot";

Word.Document oWordDoc = oWordApp.Documents.Add( ref oTemplate,ref missing,ref missing, ref missing);//新建word文档

oWordApp.Visible = true;//设置Word程序可见,如果为false 那么word 不可见

//页面设置

oWordDoc.PageSetup.TopMargin = oWordApp.CentimetersToPoints(2.5f); //上

oWordDoc.PageSetup.BottomMargin = oWordApp.CentimetersToPoints(2f);//下

oWordDoc.PageSetup.LeftMargin=oWordApp.CentimetersToPoints(2.2f);//左

oWordDoc.PageSetup.RightMargin=oWordApp.CentimetersToPoints(2.2f);//右

//添加页眉

oWordDoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader; //激活页眉的编辑

oWordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; //设置对齐方式

string headtext1 ="Head Text";

oWordApp.Selection.Font.Name ="华文新魏"; //设置字体

oWordApp.Selection.Font.Size =10.5f;

oWordApp.Selection.Font.UnderlineColor = Word.WdColor.wdColorAutomatic;

oWordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineSingle; //添加下划线

oWordApp.Selection.TypeText(headtext1);

oWordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineNone;

//添加页脚

string foottext1 ="Foot Text";

oWordDoc.ActiveWindow.ActivePane.View.SeekView =Word.WdSeekView.wdSeekCurrentPageFooter; //激活页脚的编辑

oWordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

oWordApp.Selection.Font.Name ="仿宋_GB2312";

oWordApp.Selection.Font.Size =8;

oWordApp.Selection.TypeText(foottext1);

//添加正文

oWordDoc.ActiveWindow.ActivePane.View.SeekView =Word.WdSeekView.wdSeekMainDocument;//激活页面内容的编辑

oWordApp.Selection.Font.Name ="宋体";

oWordApp.Selection.Font.Size =10.5f;

oWordApp.Selection.Font.Scaling = 200;

oWordApp.Selection.TypeText("Hello!!!");

oWordApp.Selection.TypeParagraph();//另起一段

oWordApp.Selection.Font.Name ="黑体";

oWordApp.Selection.Font.Size =10.5f;

oWordApp.Selection.Font.Scaling = 100;

oWordApp.Selection.TypeText("Hello!!!");

oWordApp.Selection.TypeParagraph();//另起一段

string strfilename = System.Windows.Forms.Application.StartupPath+"\\myfirst.doc";

object filename = strfilename ;

//保存文档为word2000格式

oWordDoc.SaveAs2000(ref filename,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);

//保存文档为word2003格式

//oWordDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing,

// ref missing, ref missing, ref missing, ref missing, ref missing,

// ref missing, ref missing, ref missing, ref missing, ref missing,

// ref missing) ;

//以下关闭Word程序

object nochanges = Word.WdSaveOptions.wdDoNotSaveChanges;

if(oWordApp.Documents!= null)

{

IEnumerator ie = oWordApp.Documents.GetEnumerator();

while( ie.MoveNext())

{

Word.Document closedoc = (Word.Document)ie.Current;

closedoc.Close(ref nochanges,ref missing,ref missing);

}

}

oWordApp.Quit(ref nochanges, ref missing, ref missing);

}

c#写word文档基础操作(自己控制样式)的更多相关文章

  1. c# word文档的操作

    参考https://blog.csdn.net/ruby97/article/details/7406806 Word对象模型  (.Net Perspective) 本文主要针对在Visual St ...

  2. 使用ABAP编程实现对微软Office Word文档的操作

    SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...

  3. [办公应用]我的WORD文档表格操作不灵活 无法调整列宽

    最近同事的一个word文档中的表格操作非常不灵活,用鼠标直接调整列宽时总觉得很不灵活.她的操作系统为XP,office 为微软office 2003. 我首先检查了木马,检查了输入法等,结果都没有问题 ...

  4. 利用Aspose.Word控件实现Word文档的操作

    Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及输出,由于一般输出的内容比较正规化或者多数是表格居多,所以一般 ...

  5. 黄聪:利用Aspose.Word控件实现Word文档的操作(转)

    撰写人:伍华聪  http://www.iqidi.com  Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...

  6. java对word文档的操作(提取标题和内容等)-直接操作或poi工具包或freemarker+xml或html转word

    1,java自带工具包实现对word的排版和写入 import java.awt.Color; import java.io.FileNotFoundException; import java.io ...

  7. java 使用 POI 操作 XWPFDocumen 创建和读取 Office Word 文档基础篇

    注:有不正确的地方还望大神能够指出,抱拳了 老铁! 参考 API:http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDoc ...

  8. XWPFDocument创建和读取Office Word文档基础篇(一)

    注:有不正确的地方还望大神能够指出,抱拳了 老铁!   参考API:http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDo ...

  9. poi操作word文档文件操作

    import org.apache.poi.POITextExtractor; import org.apache.poi.hwpf.extractor.WordExtractor; //得到.doc ...

随机推荐

  1. mongodb-help功能

    mongo-help功能 version:2.6.12下面是示例: > help db.help()                  help on db methods       db.m ...

  2. JDBC编程之程序优化

    -----------------siwuxie095 首先下载 MySQL 的 JDBC 驱动,下载链接: https://dev.mysql.com/downloads/connector/j/ ...

  3. 应用程序无法正常启动提示错误0xc000007b 问题的原因和解决方法

    应用程序无法正常启动提示错误0xc000007b 问题的原因和解决方法 前提条件: 你使用的是VS201x软件编写程序,你使用的电脑是X64位的,并且你在使用OpenCV库.你编写的程序可以正常编译, ...

  4. 阶段2-新手上路\项目-移动物体监控系统\Sprint2-摄像头子系统开发\第2节-V4L2图像编程接口深度学习

    参考资料: http://www.cnblogs.com/emouse/archive/2013/03/04/2943243.htmlhttp://blog.csdn.net/eastmoon5021 ...

  5. 4. DVWA亲测暴力破解

    LOW等级 我们先用burpsuite抓包,因为burpsuite提供了暴力破解模块   我们先创建一个1.txt文件夹,把正确的账号密码写进去   我们输入 Username:1 Password: ...

  6. ASP.NET MVC 小牛之旅1:何谓MVC

    在学习ASP.NET MVC之前首先了解什么 是MVC ? MVC不是一种语言,严格来说也不算一个技术,而是开发时所使用的一种架构(框架),它就像是一种开发观念,或是一个设计样式. MVC让软件开发的 ...

  7. C# 、.NET、ASP.NET MVC积累

    2016-10-27 给视图中的select赋值: 控制器: public ActionResult Add() { List<SelectListItem> ClassName = ne ...

  8. Python中的循环语句

    Python中有while循环和for循环 下面以一个小例子来说明一下用法,用户输入一些数字,输出这些数字中的最大值和最小值 array = [5,4,3,1] for i in array: pri ...

  9. Windows 命令行方式打印和设置变量

    echo %PATH% http://blog.csdn.net/snlei/article/details/7211770

  10. AT2161 シャッフル / Shuffling

    传送门 其实有一个显然的性质嘛:对于每个数,其实只要考虑它最右能被换到的位置就好了 然后设\(f[i][j]\)表示已经处理完了前\(i-1\)位,当前还有\(j\)个\(1\)可以自由支配(注意这里 ...