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 ...
随机推荐
- GIF助手帮助说明
生成的文件位置? 生成的GIF等文件存储在 /sdcard/gifdir/ 目录下. 为什么分享到微信.Twitter等平台失败? 微信.Twitter等平台对GIF文件大小有限制,微信限制为1MB, ...
- python tarfile模块基本使用
1.压缩一个文件夹下的所有文件 #coding=utf8 import os import tarfile __author__ = 'Administrator' def main(): cwd = ...
- Hive 启动 Diagnostic Messages for this Task: java.lang.Throwable: Child Error
Diagnostic Messages for this Task: java.lang.Throwable: Child Error at org.apache.hadoop.mapred.Task ...
- C++之string类
1.String对象的初始化 string s1; 默认构造函数,s1为空串 string s4(n, 'c'); 将s4初始化为字符c的n个副本 string s ...
- [dp]编辑距离问题
https://www.51nod.com/tutorial/course.html#!courseId=3 转移方程: 注意如何对齐的. 这个算法的特点是,S和T字符串左边始终是对齐的.为了更好地理 ...
- 【PHP】Composer使用简介,composer install 和 update 区别
1.composer是php的依赖包管理工具 2.符合PSR-0/1/2/3/4 规范 3.composer安装推荐使用国内镜像 4.composer require/install/update 区 ...
- 6.docker常用命令
docker 常见命令 更细的配置请参考官方文档 第一大部分容器生命周期管理 01 .docker run :创建一个新的容器并运行一个命令 $ docker run [OPTIONS] IMAGE ...
- VS插件开发(生成实体类)
写了N年的代码,回头过来看,其中有80%代码都是重复的代码,深入研究VS插件开发,应用到工作中,让自己减少基础的代码开发,增加开发效率 1.新增解决方案: Extensiblity->VSIX ...
- ProtoBuf练习(五)
表类型 protobuf语言的maps字段类型相当于C++语言的std::map类型 工程目录结构 $ ls proto/ sample_maps.proto proto文件 $ cat proto/ ...
- [CentOS7] 磁盘分区(gdisk, fdisk)
声明:本文主要总结自:鸟哥的Linux私房菜-第七章.Linux 磁碟與檔案系統管理,如有侵权,请通知博主 磁盘分区步骤: 1.先找出当前有哪些磁盘lsblk或blkid(也就是确定是sda还是vda ...