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的更多相关文章

  1. C# Word 类库的深入理解

    using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...

  2. C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化

    转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...

  3. Word001

    C# Word 类库 2009-08-06 22:10 13470人阅读 评论(10) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...

  4. 比较全的 C# 操作 Word的代码

    using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...

  5. C# word 类库基本属性介绍

    using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...

  6. c# word 操作

    public class WordOperate { #region 新建Word文档 /// <summary> /// 动态生成Word文档并填充内容 /// </summary ...

  7. C# Word 类库

    C# Word 类库 2009-08-06 22:10 14292人阅读 评论(11) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...

  8. 现代软件工程HW1:词频统计

    作业详细要求:http://www.cnblogs.com/denghp83/p/8627840.html 基本功能 1. 统计文件的字符数(只需要统计Ascii码,汉字不用考虑,换行符不用考虑,'\ ...

随机推荐

  1. pid 控制算法

    http://blog.csdn.net/huangkangying/article/details/78129148 https://zh.wikipedia.org/wiki/PID%E6%8E% ...

  2. MYSQL浮点型转int类型

    cast('3.15926' as signed) cast(浮点型 as signed)

  3. PHP 通过带SSL的SMTP 发送邮件的处理

    客户端与SMTP服务器的通讯, 是通过固定的命令以及返回编号完成的. 发送Email, 需要经过的步骤有创建socket (区分带ssl, 还是不带ssl)执行命令, 并检查返回值是否与预期一致, 不 ...

  4. 使用 bibtex4word 实现在 office word 中管理并插入参考文献

    使用 bibtex4word 实现在 office word 中管理并插入参考文献, 简单的步骤流程如下: 1. 下载bibtex4word.zip  (无需安装): 下载地址: http://www ...

  5. QT和MFC的差别

    QT和MFC的差别 在使用MFC之前就已经使用Qt这个事实可能影响了我的客观性. (MFC效率较高,但大量的Windows API和消息机制使得其较难理解,不易用:QT封装较好,易用且跨平台,但效率较 ...

  6. 3D打印技术之切片引擎(5)

    [此系列文章基于熔融沉积( fused depostion modeling, FDM )成形工艺] 从这一篇文章開始,就開始说填充.在3D打印切片技术中,填充算法是最核心的部分.3D打印技术的经常使 ...

  7. 菜鸟学Java(二十三)——Java内存分析

    我们常说的Java内存主要分为四大块(寄存器不在考虑之内,我们无法用代码来操控它):stack(栈).heap(堆).data segment(数据区).code segment(代码区).它们的主要 ...

  8. idea android 开发

    plugins 勾上 插件即可

  9. Series 入门(创建和增删改查)

    Series 是pandas两大数据结构中(DataFrame,Series)的一种.使用pandas 前需要将pandas 模块引入,因为Series和DataFrame用的次数非常多,所以将其引入 ...

  10. sqlmap tamter

    支持的数据库 编号 脚本名称 作用 实现方式 all 1 apostrophemask.py 用utf8代替引号 ("1 AND '1'='1") '1 AND %EF%BC%87 ...