下面一个函数,建立一个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. Elasticsearch之curl创建索引库

    关于curl的介绍,请移步 Elasticsearch学习概念之curl 启动es,请移步 Elasticsearch的前后台运行与停止(tar包方式) Elasticsearch的前后台运行与停止( ...

  2. 使用struts2进行文件下载以及下载权限控制的例子

    本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流. 下面直接上我的代码: 登录表 ...

  3. 07_ddms透视图介绍

    通过ADB(Android Debug Bridge)安卓调试桥把你的Eclipse(集成开发环境)和你的设备连接在一起.有时候ADB可能会被其他的东西占用.例如WPS会跟你抢ADB(抢端口).如果你 ...

  4. github 分支操作

    1.查看分支 1.查看本地分支 使用git branch命令,如下: $ git branch * master *标识的是你当前所在的分支. 2.查看远程分支 命令如下: git branch -r ...

  5. 【NLP】中文分词:原理及分词算法

    一.中文分词 词是最小的能够独立活动的有意义的语言成分,英文单词之间是以空格作为自然分界符的,而汉语是以字为基本的书写单位,词语之间没有明显的区分标记,因此,中文词语分析是中文信息处理的基础与关键. ...

  6. Entity Framework Code-First(10.2):Entity Mappings

    Entity Mappings using Fluent API: Here, we will learn how to configure an entity using Fluent API. W ...

  7. Entity Framework Code-First(10):Fluent API

    Fluent API in Code-First: We have seen different DataAnnotations attributes in the previous sections ...

  8. Entity Framework Code-First(8):Configure Domain Classes

    Configure Domain Classes in Code-First: We learned default Code-First Conventions in the previous se ...

  9. 面试问题 - SQL 中存储过程与函数的区别

    SQL 中的存储过程与函数没有本质上的区别 函数 -> 只能返回一个变量. 函数可以嵌入到sql中使用, 可以在select 中调用, 而存储过程不行.  但函数也有着更多的限制,比如不能使用临 ...

  10. 移除KVO的风险

    为之前项目添加一个功能用到了一个开源库XMTextView,然后运行报错提示: 显示没有注册一个叫font的观察者,所以闪退.但是我的UITextView没有添加观察者呀,怎么会删除呢? 原来是由分类 ...