//需要添加引用 Microsoft.Office.Interop.Word
public class WordHelp { Microsoft.Office.Interop.Word.Application oWordApp; Microsoft.Office.Interop.Word.Document oWordDoc; object oMissing = System.Reflection.Missing.Value; /// <summary> /// 加载文档 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public bool LoadWordDoc(string fileName) { try { oWordApp = new Microsoft.Office.Interop.Word.Application(); if (oWordApp == null) { CWConfig.ReadResource resRead = new CWConfig.ReadResource(); MessageBoxCls.Show(resRead.GetResouce("Contract.ComputeNotSetWord"), "", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } } catch (Exception) { CWConfig.ReadResource resRead = new CWConfig.ReadResource(); MessageBoxCls.Show(resRead.GetResouce("Contract.ComputeNotSetWord"), "", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } try { object readOnly = false; object isVisible = false; oWordDoc = oWordApp.Documents.Open(fileName, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oWordDoc.Activate(); } catch (Exception ex) { //ex.ToString() 2005-07-25 Tony Modify MessageBoxCls.Show(ex.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Information); oWordDoc = null; } return true; } /// <summary> /// 设置页眉 /// </summary> /// <param name="title"></param> public void SetHeader(string title) { oWordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; oWordApp.Selection.WholeStory(); oWordApp.Selection.TypeText(title); oWordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; } /// <summary> /// 替换所有同类型文本 /// </summary> /// <param name="strOldText"></param> /// <param name="strNewText"></param> /// <returns></returns> public bool SearchReplace(string strOldText, string strNewText) { object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。 oWordApp.Selection.Find.ClearFormatting(); oWordApp.Selection.Find.Text = strOldText; oWordApp.Selection.Find.Replacement.ClearFormatting(); oWordApp.Selection.Find.Replacement.Text = strNewText; if (oWordApp.Selection.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing)) { return true; } return false; } public void ReplaceTable(int tabIndex, DataTable dt, params string[] colNames) { if (oWordDoc.Tables.Count < tabIndex || dt == null || dt.Rows.Count < 1) return; Microsoft.Office.Interop.Word.Table newTable = oWordDoc.Tables[tabIndex]; int rowCount = dt.Rows.Count; while (rowCount > 0) { object beforeRow = newTable.Rows[2]; newTable.Rows.Add(ref beforeRow); rowCount--; } for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++) { DataRow dr = dt.Rows[rowIndex]; Microsoft.Office.Interop.Word.Row row = newTable.Rows[rowIndex + 2]; for (int index = 0; index < colNames.Length; index++) { string s = colNames[index]; if (dt.Columns.Contains(s)) row.Cells[index + 1].Range.Text = dr[s].ToString(); } } } /// <summary> /// 保存 /// </summary> /// <param name="filename"></param> /// <param name="path"></param> /// <returns></returns> public void Save(object filename) { oWordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oWordApp.Quit(ref oMissing, ref oMissing, ref oMissing); oWordDoc = null; oWordApp = null; GC.Collect(); } }