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码,汉字不用考虑,换行符不用考虑,'\ ...
随机推荐
- js显示屏幕分辨率
<div style=" width:88%;margin:30px auto; color:blue;" id="div_html"> </ ...
- sublime python3中读取和写入文件时如何解决编码问题
# -*- coding: utf-8 -*- #分析用户身份审核信息 #python 3.5 #xiaodeng #http://apistore.baidu.com/apiworks/servic ...
- css + div 列表布局
常见列表布局,效果如下图.常见图与图之间经常会留间距,下图图与图没留间距 1.第一种列表布局:float + margin 1.2.第一种列表布局相应代码 <!DOCTYPE html> ...
- elast alert
参考文档:<elast alert> 假设报错的内容为: ceph-rest-api service down At least 1 events occurred between 201 ...
- 《转》vue更新到2.0之后vue-resource不在更新,axios的使用
vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐的axios,前一段时间用了一下,现在说一下它的基本用法. 首先就是引入axios,如果你使用es6,只需要安装axios ...
- (4) MySQL中EXPLAIN执行计划分析
一. 执行计划能告诉我们什么? SQL如何使用索引 联接查询的执行顺序 查询扫描的数据函数 二. 执行计划中的内容 SQL执行计划的输出可能为多行,每一行代表对一个数据库对象的操作 1. ID列 ID ...
- Asp.Net 合并图片(二维码和其他图片合并)
需求:根据网址生成二维码,然后再把二维码生成到背景图片上,然后显示在网页中 pic.ashx <%@ WebHandler Language="C#" Class=" ...
- 怎么关闭win10和win8快速启动
电源选项-- 选择电源按钮的功能--- 更改当前不可用的设置-- 快速启动勾去掉
- easyui 自定义验证规则 验证用户是否已经存在 远程ajax验证
easyui远程ajax验证 2014年09月30日 22:29:32 clj198606061111 阅读数:6130 标签: easyui 更多 个人分类: jqueryeasyui 版权声明:本 ...
- 【转】WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 菜单Menu的自定义样式: 右键菜单ContextMenu的自定义样式 ...