最近程序中经常使用到word的操作,我在网上查了一些资料,在这里整理一下。
使用代码创建word文档:
#region 新建Word文档
/// <summary>
/// 动态生成Word文档并填充内容
/// </summary>
/// <param name="dir">文档目录</param>
/// <param name="fileName">文档名</param>
/// <returns>返回自定义信息</returns>
public static bool CreateWordFile(string dir, string fileName)
{
try
{
Object oMissing = System.Reflection.Missing.Value;
if (!Directory.Exists(dir))
{
//创建文件所在目录
Directory.CreateDirectory(dir);
}
//创建Word文档(Microsoft.Office.Interop.Word)
Microsoft.Office.Interop.Word._Application WordApp = new Application();
WordApp.Visible = true;
Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add(
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//保存
object filename = dir + fileName;
WordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return false;
}
}
#endregion 新建Word文档
给Word文档添加页眉页脚#region 给word文档添加页眉页脚
/// <summary>
/// 给word文档添加页眉
/// </summary>
/// <param name="filePath">文件名</param>
/// <returns></returns>
public static bool AddPageHeaderFooter(string filePath)
{
try
{
Object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application WordApp = new Application();
WordApp.Visible = true;
object filename = filePath;
Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
////添加页眉方法一:
//WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
//WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
//WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容
////添加页眉方法二:
if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
{
WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
}
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.HeaderFooter.Range.Text = "页眉内容";
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容");
//跳出页眉页脚设置
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
//保存
WordDoc.Save();
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return false;
}
}
#endregion 给word文档添加页眉页脚
在这里做个笔记,希望能帮助大家。另外 部分代码 来自于网络。
分享到:
相关推荐
C#操作Word——C#向Word文档中写入文字
批量添加 Word 文档页眉页脚的小工具.
2、如何使用C#在Word文档中插入图片以及读取Word文档中的图片?3、提取word 文档中的图片信息;4、提取WORD中的所有InlineShape图片并保存成文件;5、C#编程实现动态生成Word文档{1、2};6、C#读取Word文档的详细...
主要介绍了使用C#实现在word中插入页眉页脚的方法,是操作Word的常见方法,有一定的学习借鉴价值,需要的朋友可以参考下
C#操作word文档 C#实现Word中表格信息读取
本demo用C# 实现了了word excel visio 添加页眉页脚和内容,其中word 添加了页脚,excel添加了首行内容,visio 添加了一个矩形容器 并设置了透明背景,增加了指定字号的文字
C#操作Word文档(根据模板生成文档、替换内容、插入表格等)
批量添加 Word 文档页眉页脚的源代码。开发环境:Visula Studio 2005 + O2003PIA.MSI(Office 2003 库,压缩包中已经附带)。
OPEN XML SDK2.0 技术开发,不启动进程WINWORD.EXE进行操作OFFICE
这段代码是出于一个自身的需求,需要给word文件添加密码和删除密码,在删除密码的过程中走了不少弯路,spire免费组件又不支持页数超多的文档.现在终于完美解决.有需要的同学拿去用吧!
C#操作word页眉 可以实现不同页面不同页眉 以及分页的设置,在此只提供源码供大家参考
此程序主要展示了如何把图片插入事先设定好的书签位置之中。 下载之后可以直接运行。
第9章 C#高级属性——帮助文档和部署 理论第9章
ITextSharp5.0生成PDF(含页眉页脚的生成)
包括: C#操作Word.doc C#操作Word表格.doc C#操作WORD辅助类.txt c#操作word模板插入文字...C#操作Word在Word文档中插入图片.docx C#用word模板做文档.doc 收集的C#操作Word的各种代码。直接复制粘贴到程序里面即可。
c#根据word模板生成word文档
可实现文件夹批量页眉页脚插入,同时支持图片和文本,图片支持gif,jpg,bmp,png格式. 支持word 2003及word2007生成的文件,当然你的机子上需要安装office才可以,我的机子安装的是2007,在2003下未作测试! 因时间有限...
C#开发Android应用实战——使用Mono for Android和.NET/C#(移动与嵌入式开发技术)是一本很不错的书籍,但网上下载的多是不完整的。该版本是完整版。希望对大家有所帮助。
VS2010下c#操作word进行批量查找和替换。选择路径后程序自动进行批量查找和替换,去持子目录搜索、大小写匹配、整字匹配、通配符匹配。
C#得到word里面的书签,并向书签插入内容,将DateGridView内容作为表格插入到word书签中。