word 文档操作类,可以读出word中书签 批量替换内容,直接调用
using System;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
namespace ELO.BLL
{
/*
* Description:用于Word基本操作類
*/
public partial class Helper_Word
{
#region 私有成員
private Word.ApplicationClass _wordApplication;
private Word.Document _wordDocument;
object missing = System.Reflection.Missing.Value;
#endregion
#region 公開屬性
/// <summary>
/// ApplciationClass
/// </summary>
public Word.ApplicationClass WordApplication
{
get
{
return _wordApplication;
}
}
/// <summary>
/// Document
/// </summary>
public Word.Document WordDocument
{
get
{
return _wordDocument;
}
}
#endregion
#region 構(gòu)造函數(shù)
public Helper_Word()
{
_wordApplication = new Word.ApplicationClass();
}
public Helper_Word(Word.ApplicationClass wordApplication)
{
_wordApplication = wordApplication;
}
#endregion
#region 基本操作(新建、打開、保存、關(guān)閉)
/// <summary>
/// 新建并打開一個(gè)文檔(默認(rèn)缺省值)
/// </summary>
public void CreateAndActive()
{
_wordDocument = CreateOneDocument(missing, missing, missing, missing);
_wordDocument.Activate();
}
/// <summary>
/// 打開指定文件
/// </summary>
/// <param name="FileName">文件名(包含路徑)</param>
/// <param name="IsReadOnly">打開后是否只讀</param>
/// <param name="IsVisibleWin">打開后是否可視</param>
/// <returns>打開是否成功</returns>
public bool OpenAndActive(string FileName, bool IsReadOnly, bool IsVisibleWin)
{
if (string.IsNullOrEmpty(FileName))
{
return false;
}
try
{
_wordDocument = OpenOneDocument(FileName, missing, IsReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, IsVisibleWin, missing, missing, missing, missing);
_wordDocument.Activate();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 關(guān)閉
/// Closes the specified document or documents.
/// </summary>
public void Close()
{
if (_wordDocument != null)
{
_wordDocument.Close(ref missing, ref missing, ref missing);
_wordApplication.Application.Quit(ref missing, ref missing, ref missing);
}
}
/// <summary>
/// 保存
/// </summary>
public void Save()
{
if (_wordDocument == null)
{
_wordDocument = _wordApplication.ActiveDocument;
}
_wordDocument.Save();
}
/// <summary>
/// 保存為...
/// </summary>
/// <param name="FileName">文件名(包含路徑)</param>
public void SaveAs(string FileName)
{
if (_wordDocument == null)
{
_wordDocument = _wordApplication.ActiveDocument;
}
object objFileName = FileName;
_wordDocument.SaveAs(ref objFileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
/// <summary>
/// 新建一個(gè)Document
/// </summary>
/// <param name="template">Optional Object. The name of the template to be used for the new document. If this argument is omitted, the Normal template is used.</param>
/// <param name="newTemplate">Optional Object. True to open the document as a template. The default value is False.</param>
/// <param name="documentType">Optional Object. Can be one of the following WdNewDocumentType constants: wdNewBlankDocument, wdNewEmailMessage, wdNewFrameset, or wdNewWebPage. The default constant is wdNewBlankDocument.</param>
/// <param name="visible">Optional Object. True to open the document in a visible window. If this value is False, Microsoft Word opens the document but sets the Visible property of the document window to False. The default value is True.</param>
public Word.Document CreateOneDocument(object template, object newTemplate, object documentType, object visible)
{
return _wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
}
/// <summary>
/// 打開一個(gè)已有文檔
/// </summary>
/// <param name="FileName"></param>
/// <param name="ConfirmConversions"></param>
/// <param name="ReadOnly"></param>
/// <param name="AddToRecentFiles"></param>
/// <param name="PasswordDocument"></param>
/// <param name="PasswordTemplate"></param>
/// <param name="Revert"></param>
/// <param name="WritePasswordDocument"></param>
/// <param name="WritePasswordTemplate"></param>
/// <param name="Format"></param>
/// <param name="Encoding"></param>
/// <param name="Visible"></param>
/// <param name="OpenAndRepair"></param>
/// <param name="DocumentDirection"></param>
/// <param name="NoEncodingDialog"></param>
/// <param name="XMLTransform"></param>
/// <returns></returns>
public Word.Document OpenOneDocument(object FileName, object ConfirmConversions, object ReadOnly,
object AddToRecentFiles, object PasswordDocument, object PasswordTemplate, object Revert,
object WritePasswordDocument, object WritePasswordTemplate, object Format, object Encoding,
object Visible, object OpenAndRepair, object DocumentDirection, object NoEncodingDialog, object XMLTransform)
{
try
{
return _wordApplication.Documents.Open(ref FileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles,
ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate,
ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
}
catch
{
return null;
}
}
#endregion
#region 移動(dòng)光標(biāo)位置
/// <summary>
/// 光標(biāo)移動(dòng)到指定書簽位置,書簽不存在時(shí)不移動(dòng)
/// </summary>
/// <param name="bookMarkName"></param>
/// <returns></returns>
public bool GoToBookMark(string bookMarkName)
{
//是否存在書簽
if (_wordDocument.Bookmarks.Exists(bookMarkName))
{
object what = Word.WdGoToItem.wdGoToBookmark;
object name = bookMarkName;
GoTo(what, missing, missing, name);
return true;
}
return false;
}
/// <summary>
/// 移動(dòng)光標(biāo)
/// Moves the insertion point to the character position immediately preceding the specified item.
/// </summary>
/// <param name="what">Optional Object. The kind of item to which the selection is moved. Can be one of the WdGoToItem constants.</param>
/// <param name="which">Optional Object. The item to which the selection is moved. Can be one of the WdGoToDirection constants. </param>
/// <param name="count">Optional Object. The number of the item in the document. The default value is 1.</param>
/// <param name="name">Optional Object. If the What argument is wdGoToBookmark, wdGoToComment, wdGoToField, or wdGoToObject, this argument specifies a name.</param>
public void GoTo(object what, object which, object count, object name)
{
_wordApplication.Selection.GoTo(ref what, ref which, ref count, ref name);
}
/// <summary>
/// 向右移動(dòng)一個(gè)字符
/// </summary>
public void MoveRight()
{
MoveRight(1);
}
/// <summary>
/// 向右移動(dòng)N個(gè)字符
/// </summary>
/// <param name="num"></param>
public void MoveRight(int num)
{
object unit = Word.WdUnits.wdCharacter;
object count = num;
MoveRight(unit, count, missing);
}
/// <summary>
/// 向下移動(dòng)一個(gè)字符
/// </summary>
public void MoveDown()
{
MoveDown(1);
}
/// <summary>
/// 向下移動(dòng)N個(gè)字符
/// </summary>
/// <param name="num"></param>
public void MoveDown(int num)
{
object unit = Word.WdUnits.wdCharacter;
object count = num;
MoveDown(unit, count, missing);
}
/// <summary>
/// 光標(biāo)上移
/// Moves the selection up and returns the number of units it's been moved.
/// </summary>
/// <param name="unit">Optional Object. The unit by which to move the selection. Can be one of the following WdUnits constants: wdLine, wdParagraph, wdWindow or wdScreen etc. The default value is wdLine.</param>
/// <param name="count">Optional Object. The number of units the selection is to be moved. The default value is 1.</param>
/// <param name="extend">Optional Object. Can be either wdMove or wdExtend. If wdMove is used, the selection is collapsed to the end point and moved up. If wdExtend is used, the selection is extended up. The default value is wdMove.</param>
/// <returns></returns>
public int MoveUp(object unit, object count, object extend)
{
return _wordApplication.Selection.MoveUp(ref unit, ref count, ref extend);
}
/// <summary>
/// 光標(biāo)下移
/// Moves the selection down and returns the number of units it's been moved.
/// 參數(shù)說明詳見MoveUp
/// </summary>
public int MoveDown(object unit, object count, object extend)
{
return _wordApplication.Selection.MoveDown(ref unit, ref count, ref extend);
}
/// <summary>
/// 光標(biāo)左移
/// Moves the selection to the left and returns the number of units it's been moved.
/// 參數(shù)說明詳見MoveUp
/// </summary>
public int MoveLeft(object unit, object count, object extend)
{
return _wordApplication.Selection.MoveLeft(ref unit, ref count, ref extend);
}
/// <summary>
/// 光標(biāo)右移
/// Moves the selection to the left and returns the number of units it's been moved.
/// 參數(shù)說明詳見MoveUp
/// </summary>
public int MoveRight(object unit, object count, object extend)
{
return _wordApplication.Selection.MoveRight(ref unit, ref count, ref extend);
}
#endregion
#region 查找、替換
/// <summary>
/// 替換書簽內(nèi)容
/// </summary>
/// <param name="bookMarkName">書簽名</param>
/// <param name="text">替換后的內(nèi)容</param>
public void ReplaceBookMark(string bookMarkName, string text)
{
bool isExist = GoToBookMark(bookMarkName);
if (isExist)
{
InsertText(text);
}
}
/// <summary>
/// 替換
/// </summary>
/// <param name="oldText">待替換的文本</param>
/// <param name="newText">替換后的文本</param>
/// <param name="replaceType">All:替換所有、None:不替換、FirstOne:替換第一個(gè)</param>
/// <param name="isCaseSensitive">大小寫是否敏感</param>
/// <returns></returns>
public bool Replace(string oldText, string newText, string replaceType, bool isCaseSensitive)
{
if (_wordDocument == null)
{
_wordDocument = _wordApplication.ActiveDocument;
}
object findText = oldText;
object replaceWith = newText;
object wdReplace;
object matchCase = isCaseSensitive;
switch (replaceType)
{
case "All":
wdReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
break;
case "None":
wdReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
break;
case "FirstOne":
wdReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;
break;
default:
wdReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;
break;
}
_wordDocument.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式設(shè)置
return _wordDocument.Content.Find.Execute(ref findText, ref matchCase, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceWith,
ref wdReplace, ref missing, ref missing, ref missing, ref missing);
}
#endregion
#region 插入、刪除操作
/// <summary>
/// 插入文本 Inserts the specified text.
/// </summary>
/// <param name="text"></param>
public void InsertText(string text)
{
_wordApplication.Selection.TypeText(text);
}
/// <summary>
/// Enter(換行) Inserts a new, blank paragraph.
/// </summary>
public void InsertLineBreak()
{
_wordApplication.Selection.TypeParagraph();
}
/// <summary>
/// 插入圖片(圖片大小自適應(yīng))
/// </summary>
/// <param name="fileName">圖片名(包含路徑)</param>
public void InsertPic(string fileName)
{
object range = _wordApplication.Selection.Range;
InsertPic(fileName, missing, missing, range);
}
/// <summary>
/// 插入圖片
/// </summary>
/// <param name="fileName">圖片名(包含路徑)</param>
/// <param name="width">設(shè)置寬度</param>
/// <param name="height">設(shè)置高度</param>
public void InsertPic(string fileName, float width, float height)
{
object range = _wordApplication.Selection.Range;
InsertPic(fileName, missing, missing, range, width, height);
}
/// <summary>
/// 插入圖片(帶標(biāo)題)
/// </summary>
/// <param name="fileName">圖片名(包含路徑)</param>
/// <param name="width">設(shè)置寬度</param>
/// <param name="height">設(shè)置高度<</param>
/// <param name="caption">標(biāo)題或備注文字</param>
public void InsertPic(string fileName, float width, float height, string caption)
{
object range = _wordApplication.Selection.Range;
InsertPic(fileName, missing, missing, range, width, height, caption);
}
/// <summary>
/// 插入圖片(帶標(biāo)題)
/// </summary>
public void InsertPic(string FileName, object LinkToFile, object SaveWithDocument, object Range, float Width, float Height, string caption)
{
_wordApplication.Selection.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Range).Select();
if (Width > 0)
{
_wordApplication.Selection.InlineShapes[1].Width = Width;
}
if (Height > 0)
{
_wordApplication.Selection.InlineShapes[1].Height = Height;
}
object Label = Word.WdCaptionLabelID.wdCaptionFigure;
object Title = caption;
object TitleAutoText = "";
object Position = Word.WdCaptionPosition.wdCaptionPositionBelow;
object ExcludeLabel = true;
_wordApplication.Selection.InsertCaption(ref Label, ref Title, ref TitleAutoText, ref Position, ref ExcludeLabel);
MoveRight();
}
/// <summary>
/// Adds a picture to a document.
/// </summary>
/// <param name="FileName">Required String. The path and file name of the picture.</param>
/// <param name="LinkToFile">Optional Object. True to link the picture to the file from which it was created. False to make the picture an independent copy of the file. The default value is False.</param>
/// <param name="SaveWithDocument">Optional Object. True to save the linked picture with the document. The default value is False.</param>
/// <param name="Range">Optional Object. The location where the picture will be placed in the text. If the range isn't collapsed, the picture replaces the range; otherwise, the picture is inserted. If this argument is omitted, the picture is placed automatically.</param>
/// <param name="Width">Sets the width of the specified object, in points. </param>
/// <param name="Height">Sets the height of the specified inline shape. </param>
public void InsertPic(string FileName, object LinkToFile, object SaveWithDocument, object Range, float Width, float Height)
{
_wordApplication.Selection.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Range).Select();
_wordApplication.Selection.InlineShapes[1].Width = Width;
_wordApplication.Selection.InlineShapes[1].Height = Height;
MoveRight();
}
/// <summary>
/// Adds a picture to a document.
/// </summary>
/// <param name="FileName">Required String. The path and file name of the picture.</param>
/// <param name="LinkToFile">Optional Object. True to link the picture to the file from which it was created. False to make the picture an independent copy of the file. The default value is False.</param>
/// <param name="SaveWithDocument">Optional Object. True to save the linked picture with the document. The default value is False.</param>
/// <param name="Range">Optional Object. The location where the picture will be placed in the text. If the range isn't collapsed, the picture replaces the range; otherwise, the picture is inserted. If this argument is omitted, the picture is placed automatically.</param>
public void InsertPic(string FileName, object LinkToFile, object SaveWithDocument, object Range)
{
_wordApplication.Selection.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Range);
}
/// <summary>
/// 插入書簽
/// 如過存在同名書簽,則先刪除再插入
/// </summary>
/// <param name="bookMarkName">書簽名</param>
public void InsertBookMark(string bookMarkName)
{
存在則先刪除
//if (_wordDocument.Bookmarks.Exists(bookMarkName))
//{
// DeleteBookMark(bookMarkName);
//}
object range = _wordApplication.Selection.Range;
_wordDocument.Bookmarks.Add(bookMarkName, ref range);
}
/ <summary>
/ 刪除書簽
/ </summary>
/ <param name="bookMarkName">書簽名</param>
//public void DeleteBookMark(string bookMarkName)
//{
// if (_wordDocument.Bookmarks.Exists(bookMarkName))
// {
// WordDocument bookMarks = _wordDocument.Bookmarks;
// for (int i = 1; i <= bookMarks.Count; i++)
// {
// object index = i;
// object bookMark = bookMarks.get_Item(ref index);
// if (bookMark.Name == bookMarkName)
// {
// bookMark.Delete();
// break;
// }
// }
// }
//}
/ <summary>
/ 刪除所有書簽
/ </summary>
//public void DeleteAllBookMark()
//{
// for (; _wordDocument.Bookmarks.Count > 0; )
// {
// object index = _wordDocument.Bookmarks.Count;
// object bookmark = _wordDocument.Bookmarks.get_Item(ref index);
// bookmark.Delete();
// }
//}
#endregion
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/luyiwei/p/3750228.html
總結(jié)
以上是生活随笔為你收集整理的word 文档操作类,可以读出word中书签 批量替换内容,直接调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vector和ArrayList区别
- 下一篇: 《30天自制操作系统》笔记(04)——显