WordOperate
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Common
{
public class WordOperate : IDisposable
{
private Microsoft.Office.Interop.Word._Application _app;
private Microsoft.Office.Interop.Word._Document _doc;
object _nullobj = System.Reflection.Missing.Value;
/// <summary>
/// 关闭Word进程
/// </summary>
public void KillWinword()
{
var p = Process.GetProcessesByName("WINWORD");
if (p.Any()) p[].Kill();
}
/// <summary>
/// 打开word文档
/// </summary>
/// <param name="filePath"></param>
public void Open(string filePath)
{
//_app = new Microsoft.Office.Interop.Word.ApplicationClass();//原版
_app = new Microsoft.Office.Interop.Word.Application();
object file = filePath;
_doc = _app.Documents.Open(
ref file, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj);
} /// <summary>
/// 替换word中的文字
/// </summary>
/// <param name="strOld">查找的文字</param>
/// <param name="strNew">替换的文字</param>
public void Replace(string strOld, string strNew)
{
//替换全局Document
_app.Selection.Find.ClearFormatting();
_app.Selection.Find.Replacement.ClearFormatting();
_app.Selection.Find.Text = strOld;
_app.Selection.Find.Replacement.Text = strNew;
object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
_app.Selection.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref objReplace, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj);
//替换页脚的字
foreach (Microsoft.Office.Interop.Word.Section wordSection in _doc.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Find.ClearFormatting();
footerRange.Find.Replacement.ClearFormatting();
footerRange.Find.Text = strOld;
footerRange.Find.Replacement.Text = strNew;
footerRange.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref objReplace, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj);
}
//替换页眉的字
foreach (Microsoft.Office.Interop.Word.Section section in _doc.Sections)
{
Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Find.ClearFormatting();
headerRange.Find.Replacement.ClearFormatting();
headerRange.Find.Text = strOld;
headerRange.Find.Replacement.Text = strNew;
headerRange.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref objReplace, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj);
}
//文本框
Microsoft.Office.Interop.Word.StoryRanges storyRanges = _doc.StoryRanges;
foreach (Microsoft.Office.Interop.Word.Range range in storyRanges)
{
Microsoft.Office.Interop.Word.Range rangeFlag = range;
if (Microsoft.Office.Interop.Word.WdStoryType.wdTextFrameStory == rangeFlag.StoryType)
{
while (rangeFlag != null)
{
rangeFlag.Find.ClearFormatting();
rangeFlag.Find.Replacement.ClearFormatting();
rangeFlag.Find.Text = strOld;
rangeFlag.Find.Replacement.Text = strNew;
rangeFlag.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj,
ref _nullobj, ref objReplace, ref _nullobj,
ref _nullobj, ref _nullobj, ref _nullobj);
rangeFlag = range.NextStoryRange;
}
}
} }
/// <summary>
/// 保存
/// </summary>
public void Save(bool disposet = true)
{
if (disposet == false)
{
_doc.Save();
}
else
{
this.Save(false);
this.Dispose();
this.KillWinword();
}
}
/// <summary>
/// 退出
/// </summary>
public void Dispose()
{
_doc.Close(ref _nullobj, ref _nullobj, ref _nullobj);
_app.Quit(ref _nullobj, ref _nullobj, ref _nullobj);
}
}
}
WordOperate的更多相关文章
- C# Word 类库的深入理解
using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...
- Word001
C# Word 类库 2009-08-06 22:10 13470人阅读 评论(10) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...
- 比较全的 C# 操作 Word的代码
using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...
- C# word 类库基本属性介绍
using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...
- c# word 操作
public class WordOperate { #region 新建Word文档 /// <summary> /// 动态生成Word文档并填充内容 /// </summary ...
- C# Word 类库
C# Word 类库 2009-08-06 22:10 14292人阅读 评论(11) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...
- 现代软件工程HW1:词频统计
作业详细要求:http://www.cnblogs.com/denghp83/p/8627840.html 基本功能 1. 统计文件的字符数(只需要统计Ascii码,汉字不用考虑,换行符不用考虑,'\ ...
随机推荐
- 关于FMDatabase executeQuery的问题
如果你碰到这个问题,请查看idx的值,并查看SQL语句中第6个字段的值有问题(从0开始),比如你给的值是NSInteger会报错,需要将其转成NSString.
- 启动apache (OS 10022)提供了一个无效的參数。解决方式
今天 apache 突然启动不起来了,查看了一下错误日志发现了例如以下错误: [Tue Mar 17 11:27:32 2015] [crit] Parent: child process exite ...
- 〖Android〗我的ADT Eclipse定制
1. 配置自动补全: Windows -> preferences -> 搜索assist,修改 java xml自动触发补全:.abcdefghijklmnopqrstuvwxyzABC ...
- android开发之打包签名
android开发之数字签名 http://www.cnblogs.com/fengzhblog/archive/2013/05/06/3063104.html Android系统要求所有的程序经过数 ...
- 阿里云日志服务采集自建Kubernetes日志(标准输出日志)
日志服务支持通过Logtail采集Kubernetes集群日志,并支持CRD(CustomResourceDefinition)进行采集配置管理.本文主要介绍如何安装并使用Logtail采集Kuber ...
- JDK自带jvisualvm监控工具
定位 工具位于jdk\bin目录下(双击运行): 可以本地或者远程调试,本地调试会自动加载,远程调试需要配置.下面以Tomcat为例. 概述 监视 线程 抽样器 Java 堆 百度经验参考:https ...
- 当 Visual Studio 扩展遇到错误时
我是遇到了 Github 扩展经常在 Visual Studio 启动时报错,找了一下可以尝试以下方法: 首先卸载插件 然后删除 %LocalAppData%\Microsoft\VisualStud ...
- Windows环境下安装PIL(Python Imaging Library)库
微信小程序--跳一跳最近火了一把,于是整了个辅助进行试玩,不过在运行程序过程中出现了个报错如图所示: 显然是缺少PIL(Python Imaging Library)库文件,于是通过pip命令行进行安 ...
- python列表中元素插入位置总结
要完成的操作是把一个列表里的元素通过for循环添加到另外一个列表里,但是通过insert()方法添加到另外一个列表后却发现元素的位置与原始列表的颠倒了.如以下实例: li1 = ['] li2 = [ ...
- android开发(49) Android 下拉刷新的实现。使用 SwipeRefreshLayout 代替 pull-to-refesh
概述 谷歌官方推出了SwipeRefreshLayout 来实现下拉刷新的效果.对比以前我们常用的 pull-to-refesh ,这个方案显得更加的简单方便. 关联项目引用(管理依赖) 在你的 应用 ...