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码,汉字不用考虑,换行符不用考虑,'\ ...
随机推荐
- 常见网站CSS样式重置
腾讯 1 2 3 4 5 6 7 8 9 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea, ...
- Linux下的两种磁盘分区工具的使用
如何使用fdisk和parted分区工具来进行硬盘分区,下面我来说一下在Linux系统中这两种硬盘分区工具的使用方法: ----------fdisk分区工具---------- ...
- [转] Dangers of using dlsym() with RTLD_NEXT
There are times when you want to wrap a library function in order to provide some additional functio ...
- mysql乱码问题解决办法
最近开发一下小项目,遇到了最常见的乱码问题. 1.数据库使用utf-8 utf-8_generic_ci编码,使用csv上传并导入数据,插入数据的时候出现了问题,有很大部分数据没有被导入,所以使用m ...
- 最近对latin-1这个字符集产生了不少好感
[简介] 最近我要解析一个数据库中间件的日志.这个中间件会在日志中记录SQL发往的后台DB ,执行耗时,对应的SQL:中间件直接把SQL写到 了日志中去,并没有对SQL进行适当的编码转换:理想情况下这 ...
- Python中的zip()与*zip()函数详解
前言 实验环境: Python 3.6: 示例代码地址:下载示例: 本文中元素是指列表.元组.字典等集合类数据类型中的下一级项目(可能是单个元素或嵌套列表). zip(*iterables)函数详解 ...
- 《转》Babel 入门教程
ECMAScript 6是JavaScript语言的下一代标准,已经在2015年6月正式发布了.Mozilla公司将在这个标准的基础上,推出JavaScript 2.0.ES6的目标,是使得JavaS ...
- shell生成连续天数日期
#!/bin/bash #两个参数:起始时间和终止时间,循环输出每天 #输入格式:20171201 20171225 #输出格式:2017-12-01 2017-12-25 startdate=`da ...
- Android Launcher分析和修改8——AllAPP界面拖拽元素(PagedViewWithDraggableItems)
接着上一篇文章,继续分析AllAPP列表界面.上一篇文章分析了所有应用列表的界面构成以及如何通过配置文件修改属性.今天主要是分析PagedViewWithDraggableItems类,因为在我们分析 ...
- 【iCore1S 双核心板_FPGA】例程十一:Modelsim仿真实验
实验现象: 通过仿真波形,分析输入与输出的关系,可以清晰的看到所添加信号波形的变化与程序所写的一致. 核心代码: module modelsim( input CLK_12M, output FPGA ...