1. 首先引入Microsoft Word 14.0 Object Library version 8.5组件,该组件在安装完office后在COM中出现
2. 其次将引用中的Microsoft.Office.Interop.Word项的嵌入式互操作设为 False
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace Mag.Compaction.TestReport { public class WordMgr : 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.Length > 0) p[0].Kill(); } /// <summary> /// 打开word文档 /// </summary> /// <param name="filePath"></param> public void Open(string filePath) { _app = new Microsoft.Office.Interop.Word.ApplicationClass(); 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 ReplaceString(string strOld, string strNew) { _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); } /// <summary> /// 替换Word中文字为图片 /// </summary> /// <param name="oldStr">需要被替换的文字</param> /// <param name="picFileName">图片路径</param> public void ReplacePicture(string oldStr, string picFileName) { string picfileName = picFileName; _app.Selection.Find.ClearFormatting(); _app.Selection.Find.Text = oldStr; _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 _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj); object Anchor = _app.Selection.Range; Anchor = _app.Selection.Range; object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; object LinkToFile = false; object SaveWithDocument = true; _doc.Shapes.AddPicture(picfileName, ref LinkToFile, ref SaveWithDocument, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref Anchor); _app.Selection.Find.Replacement.ClearFormatting(); _app.Selection.Find.Replacement.Text = ""; _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); } /// <summary> /// 保存 /// </summary> public void Save() { _doc.Save(); } /// <summary> /// 另存为 /// </summary> /// <param name="strFileName"></param> public void SaveAs(string strFileName) { _doc.SaveAs(strFileName, 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> /// 退出 /// </summary> public void Dispose() { _doc.Close(ref _nullobj, ref _nullobj, ref _nullobj); _app.Quit(ref _nullobj, ref _nullobj, ref _nullobj); } } }