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 ...
随机推荐
- SharePoint Project Server List 列表CURD操作使用rest api接口
//#region 界面交互代码 var issuesUtils = issuesUtils || {}; (function () { /** * 点击问题提处理方案按钮事件 */ issuesUt ...
- springmvc chrome jsonviewer 一起请求 重复提提交 controller重复执行 2次执行
情况一: springmvc chrome jsonviewer 一起请求 重复提提交 controller重复执行 2次执行 Chrome浏览器地址栏访问接口url,重复请求问题解决 不论刷新还 ...
- [HDU1754]I Hate It线段树裸题
http://acm.hdu.edu.cn/showproblem.php?pid=1754 解题关键:刚开始死活超时,最后发现竟然是ch,和t1.t2每次循环都定义的锅,以后养成建全局变量的习惯. ...
- 为JFileChooser设定扩展名过滤
--------------------siwuxie095 工程名:TestFileChooser 包名:com.siwuxie095.fil ...
- sell01 环境搭建、编写持久层并进行测试
1 环境配置 JDK 1.8 MAVEN 3.5 MYSQL 5.7 VirtualBox 5.1 2 搭建MYSQL环境 下载 VM 和 虚拟镜像文件 虚拟镜像文件:点击前往 技巧01:安装完vir ...
- 8、泛型程序设计与c++标准模板库2.2向量容器
向量容器属于顺序容器,用于容纳不定长线性序列(即线性群体),提供对序列的快速随机访问(也称直接访问).这一点与c++语言支持的基本数组类型相同,但基本数据类型不是面向对象的.而面向对象的向量是动态结构 ...
- hdu1064
#include <iostream>#include<iomanip>using namespace std; int main(){ float total_sum=0.0 ...
- 关于js模板引擎template的使用记录
引言 有一天在群里有一个人发了这么一个图片 看到这个就会发现2个问题,一个是后期如果html结构改变了,这一大块都要重写.还有一个就是写的时候自己都看不清,很容易出错. 然后还有一个人写的清楚一点,但 ...
- Spring Security 入门原理及实战
目录 从一个Spring Security的例子开始 创建不受保护的应用 加入spring security 保护应用 关闭security.basic ,使用form表单页面登录 角色-资源 访问控 ...
- 51nod1042(0-x出现次数&分治)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042 题意:中文题诶- 思路:这道题和前面的51nod100 ...