c#写word文档基础操作(自己控制样式)
下面一个函数,建立一个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文档基础操作(自己控制样式)的更多相关文章
- c# word文档的操作
参考https://blog.csdn.net/ruby97/article/details/7406806 Word对象模型 (.Net Perspective) 本文主要针对在Visual St ...
- 使用ABAP编程实现对微软Office Word文档的操作
SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...
- [办公应用]我的WORD文档表格操作不灵活 无法调整列宽
最近同事的一个word文档中的表格操作非常不灵活,用鼠标直接调整列宽时总觉得很不灵活.她的操作系统为XP,office 为微软office 2003. 我首先检查了木马,检查了输入法等,结果都没有问题 ...
- 利用Aspose.Word控件实现Word文档的操作
Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及输出,由于一般输出的内容比较正规化或者多数是表格居多,所以一般 ...
- 黄聪:利用Aspose.Word控件实现Word文档的操作(转)
撰写人:伍华聪 http://www.iqidi.com Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...
- java对word文档的操作(提取标题和内容等)-直接操作或poi工具包或freemarker+xml或html转word
1,java自带工具包实现对word的排版和写入 import java.awt.Color; import java.io.FileNotFoundException; import java.io ...
- java 使用 POI 操作 XWPFDocumen 创建和读取 Office Word 文档基础篇
注:有不正确的地方还望大神能够指出,抱拳了 老铁! 参考 API:http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDoc ...
- XWPFDocument创建和读取Office Word文档基础篇(一)
注:有不正确的地方还望大神能够指出,抱拳了 老铁! 参考API:http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDo ...
- poi操作word文档文件操作
import org.apache.poi.POITextExtractor; import org.apache.poi.hwpf.extractor.WordExtractor; //得到.doc ...
随机推荐
- CentOS7下yum方式安装mysql5.6
在Centos7中用MariaDB代替了mysql数据库.所以在新安装MySQL前必须做好对系统的清理工作. 一.清理CentOS7下的MariaDB. [root@localhost ~]#rpm ...
- 【Java】Java程序员面试宝典(第三版)第5章----Java程序设计基本概念
1.static静态变量,在次级作用域也可以被修改. 2.k++ + k++.第一个自加实际上只有在与计算+k++时补增.详情P36的题目. 3.Java数据类型从低到高分为(byte short c ...
- ResultSetMetaData和ResultSet
我现在有一张表t_product;我们查询所有的商品:SELECT * FROM t_product; 上述所有的数据都可以封装成一个对象,我们称这个查询出来的对象为结果集对象:ResultSet. ...
- ubuntu下root用户默认密码及修改方法
[ubuntu下root用户默认密码及修改方法] 很多朋友用ubuntu,一般都是装完ubuntu系统,马上就修改root密码了,那么root用户的默认密码是多少,当忘记root用户密码时如何找回呢, ...
- 第七篇 elasticsearch 链接mysql不会更新
这是我键的索引 "settings":{ "number_of_shards":3, "number_of_replicas":2 }, & ...
- C++中队列的建立和操作
什么是队列结构 队列结构是从数据运算来分类的,也就是说队列结构具有特殊的运算规则.而从数据的逻辑结构来看,队列结构其实就是一种线性结构.如果从数据的存储结构来进一步划分,队列结构可以分成两类. 顺序队 ...
- 软件开发中对MVC的一些理解
接口隔离 通俗一点讲,接口隔离就是当一个类需要继承另一个类时, 如果被继承的类中有继承的类用不到的方法或者属性时,就不要去实现这个继承.如果真的情非得已必须要继承,那么也需要从被继承的类中再提取出一个 ...
- Linux下ping加上时间戳
命令(ping10次): ping 192.168.5.160 -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S", ...
- adnroid 启动是没有标题栏
<activity android:name=".MainActivity" android:theme="@android:style/Theme.Light.N ...
- 程序员笔记|详解Eureka 缓存机制
引言 Eureka是Netflix开源的.用于实现服务注册和发现的服务.Spring Cloud Eureka基于Eureka进行二次封装,增加了更人性化的UI,使用更为方便.但是由于Eureka本身 ...