WORD操作类

来源:互联网 发布:美发软件下载 编辑:程序博客网 时间:2024/05/29 19:51

WORD操作类
  =======================================================  
  using   System;  
  using   System.Data;  
  using   System.Reflection;  
  using   System.IO;  
  using   Microsoft.Office.Core;  
  using   System.Windows.Forms;  
  using   Word   =   Microsoft.Office.Interop.Word;  
   
  namespace   compass.Common  
  {  
          ///   <summary>  
          ///   作者:emanlee  
          ///   功能描述:对Word进行操作  
          ///   创建时间:2006-04-7  
          ///   说明:在工程中需要添加   Word   11.0对象库的引用(Office   2000为Word   9.0,Office   XP为Word   10.0);  
          ///   方法:添加引用-com-microsoft   word   11.0   object   library;  
          ///       需要在Dcom中配置Wordl应用程序的权限;  
          ///       服务器需要安装Office2003  
          ///   help:Visual   Studio   Tools   for   Office   文档—〉使用   Office   对象模型实现应用程序自动化   —〉Word   任务    
          ///   目录:office开发:开发工具和语言:visual   studio文档:Visual   Studio   Tools   for   Office   文档—〉  
          ///   使用   Office   对象模型实现应用程序自动化   —〉Word   任务    
          ///   </summary>  
          public   class   WordLib  
          {  
   
                  #region   Variables  
                  private   Word.Application   WordApp   =   null;  
                  private   string   WordOpenFileName   =   ""; //操作Word的路径  
                  private   string   WordSaveFileName   =   ""; //保存Word的路径  
                  object   missing   =   System.Type.Missing;  
                  #endregion  
   
                  #region   Properties  
   
                  public   string   OpenFileName  
                  {  
                          get  
                          {  
                                  return   WordOpenFileName;  
                          }  
                          set  
                          {  
                                  WordOpenFileName   =   value;  
                          }  
                  }  
                  public   string   SaveFileName  
                  {  
                          get  
                          {  
                                  return   WordSaveFileName;  
                          }  
                          set  
                          {  
                                  WordSaveFileName   =   value;  
                          }  
                  }  
                  #endregion  
   
                  //  
                  //--------------------------------------------------------------------------------------------------------  
                  ///   <summary>  
                  ///   构造函数;  
                  ///   </summary>  
                  public   WordLib()  
                  {  
                  }  
                  public   bool   NewWordApp()  
                  {  
                          if   (WordApp   !=   null)   CloseWordApp();  
                          WordApp   =   new   Word.ApplicationClass();  
                          WordApp.Visible   =   false;  
                          return   true;  
                  }  
   
                  public   bool   NewWordFile()  
                  {  
                          //创建基于   Normal.dot   的新文档  
                          object   missing   =   System.Type.Missing;  
                          //object   missing   =   System.Type.Missing;  
                          WordApp.Documents.Add(ref   missing,   ref   missing,   ref   missing,   ref   missing);  
                          return   true;  
                  }  
                  public   bool   NewWordFile(object   template)  
                  {  
                          //使用自定义模板  
                          //首先检查模板文件是否存在  
                          if   (template.ToString()   ==   "")  
                          {  
                                  throw   new   Exception("请选择文件!");  
   
                          }  
                          if   (!File.Exists(template.ToString()))  
                          {  
                                  throw   new   Exception(template   +   "该文件不存在!");//该异常如何处理,由什么处理????  
   
                          }  
                          //object   template   =   @"C:/Test/SampleTemplate.dot";  
                          object   missing   =   System.Type.Missing;  
                          WordApp.Documents.Add(ref   template,   ref   missing,   ref   missing,   ref   missing);  
                          return   true;  
                  }  
   
                  public   bool   SaveWordFile(object   fileName)  
                  {  
                          //保存与项目关联的文档  
                          Word.DocumentClass   doc   =   WordApp.Documents.get_Item(ref   fileName)   as   Word.DocumentClass;  
                          doc.Save();  
                          return   true;  
                  }  
                  public   bool   SaveActiveWordFile()  
                  {  
                          //保存活动文档  
                          WordApp.ActiveDocument.Save();  
                          return   true;  
                  }  
   
                  public   bool   SaveWordFileAS(object   fileName)  
                  {  
                          //使用   SaveAs   保存文档  
                          //object   fileName   =   @"C:/Test/NewDocument.doc";  
   
                          WordApp.ActiveDocument.SaveAs(ref   fileName,  
                                  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);  
                          return   true;  
                  }  
                  public   bool   CloseWordFile(object   fileName)  
                  {  
                          //在不提示用户的前提下关闭文档并保存所做的更改  
                          //fileName   =   "NewDocument.doc";  
                          object   doNotSaveChanges   =   Word.WdSaveOptions.wdDoNotSaveChanges;  
                          Word.DocumentClass   doc   =   WordApp.Documents.get_Item(ref   fileName)   as   Word.DocumentClass;  
                          object   missing   =   System.Type.Missing;  
                          doc.Close(ref   doNotSaveChanges,   ref   missing,   ref   missing);  
                          return   true;  
                  }  
   
                  //--------------------------------------------------------------------------------------------------------  
                  ///   <summary>  
                  ///   关闭Word文件,释放对象;最后一定要调用此函数,否则会引起异常  
                  ///   </summary>  
                  ///   <param></param>    
                  public   void   CloseWordApp()  
                  {  
                          try  
                          {  
                                  if   (WordApp   !=   null)  
                                  {  
                                          WordApp   =   null;  
                                  }  
                          }  
                          finally  
                          {  
                                  GC.Collect();  
                                  GC.WaitForPendingFinalizers();  
                                  GC.Collect();  
                                  GC.WaitForPendingFinalizers();  
                          }  
                  }  
                  public   void   Quit()  
                  {  
                          object   missing   =   System.Type.Missing;  
                          WordApp.Application.Quit(ref     missing,   ref     missing,   ref     missing);  
                  }  
   
                  public   void   Save()  
                  {  
                            WordApp.ActiveDocument.Save();  
                  }  
                  public   void   SaveAs(string   strFileName)  
                  {  
                          object   missing   =   System.Type.Missing;  
                          object   fileName   =   strFileName;  
                            WordApp.ActiveDocument.SaveAs(ref     fileName,   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);  
                  }  
   
                  public   void   SaveAsHtml(string   strFileName)  
                  {  
                          object   missing   =   System.Type.Missing;  
                          object   fileName   =   strFileName;  
                          object   Format   =   (int)Word.WdSaveFormat.wdFormatHTML;  
                            WordApp.ActiveDocument.SaveAs(ref     fileName,   ref     Format,   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);  
                            WordApp.ActiveDocument.Close(ref     missing,   ref     missing,   ref     missing);  
   
                  }  
                  public   void   CopyAll()  
                  {  
                          WordApp.Selection.WholeStory();  
                          WordApp.Selection.Copy();  
   
                  }  
public   void   PasetAll()  
                  {  
   
                          WordApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);  
   
                  }  
                  public   void   Clear()  
                  {  
                          //删除整个文档的内容  
                          object   Unit   =   (int)Word.WdUnits.wdCharacter;  
                          object   Count   =   1;  
                          WordApp.Selection.WholeStory();  
                          WordApp.Selection.Delete(ref     Unit,   ref     Count);  
                  }  
   
                  public   void   InsertText(string   strText)  
                  {  
                          WordApp.Selection.TypeText(strText);  
                  }  
                  public   void   InsertLineBreak()  
                  {  
                          WordApp.Selection.TypeParagraph();  
                  }  
                  public   void   InsertLineBreak(int   nline)  
                  {  
                          for   (int   i   =   0;   i   <   nline;   i++)  
                                  WordApp.Selection.TypeParagraph();  
                  }  
   
                  public   void   SetAlignment(string   strType)  
                  {  
                          switch   (strType)  
                          {  
                                  case   "Center":  
                                          WordApp.Selection.ParagraphFormat.Alignment   =   Word.WdParagraphAlignment.wdAlignParagraphCenter;  
                                          break;  
                                  case   "Left":  
                                          WordApp.Selection.ParagraphFormat.Alignment   =   Word.WdParagraphAlignment.wdAlignParagraphLeft;  
                                          break;  
                                  case   "Right":  
                                          WordApp.Selection.ParagraphFormat.Alignment   =   Word.WdParagraphAlignment.wdAlignParagraphRight;  
                                          break;  
                                  case   "Justify":  
                                          WordApp.Selection.ParagraphFormat.Alignment   =   Word.WdParagraphAlignment.wdAlignParagraphJustify;  
                                          break;  
                          }  
                  }  
   
   
                  public   void   SetFont(string   strType)  
                  {  
                          switch   (strType)  
                          {  
                                  case   "Bold":  
                                          WordApp.Selection.Font.Bold   =   1;  
                                          break;  
                                  case   "Italic":  
                                          WordApp.Selection.Font.Italic   =   1;  
                                          break;  
                                  case   "Underlined":  
                                          WordApp.Selection.Font.Subscript   =   0;  
                                          break;  
                          }  
   
                  }  
   
   
                  public   void   SetFont()  
                  {  
                          WordApp.Selection.Font.Bold   =   0;  
                          WordApp.Selection.Font.Italic   =   0;  
                          WordApp.Selection.Font.Subscript   =   0;  
   
                  }  
                  public   void   SetFontName(string   strType)  
                  {  
                          WordApp.Selection.Font.Name   =   strType;  
   
                  }  
                  public   void   SetFontSize(int   nSize)  
                  {  
                          WordApp.Selection.Font.Size   =   nSize;  
   
                  }  
                  public   void   InsertPagebreak()  
                  {  
   
                          object   pBreak   =   (int)Word.WdBreakType.wdPageBreak;  
                          WordApp.Selection.InsertBreak(ref     pBreak);  
                  }  
   
                  public   void   GotoBookMark(string   strBookMarkName)  
                  {  
   
                          object   missing   =   System.Type.Missing;  
                          object   Bookmark   =   (int)Word.WdGoToItem.wdGoToBookmark;  
                          object   NameBookMark   =   strBookMarkName;  
                          WordApp.Selection.GoTo(ref     Bookmark,   ref     missing,   ref     missing,   ref     NameBookMark);  
                  }  
                  public   void   GoToTheEnd()  
                  {  
   
                          object   missing   =   System.Type.Missing;  
                          object   unit;  
                          unit   =   Word.WdUnits.wdStory;  
                          WordApp.Selection.EndKey(ref     unit,   ref     missing);  
   
                  }  
                  public   void   GoToTheBeginning()  
                  {  
   
                          object   missing   =   System.Type.Missing;  
                          object   unit;  
                          unit   =   Word.WdUnits.wdStory;  
                          WordApp.Selection.HomeKey(ref     unit,   ref     missing);  
   
                  }  
                  public   void   GoToTheTable(int   ntable)  
                  {  
                          object   missing   =   System.Type.Missing;  
                          object   what;  
                          what   =   Word.WdUnits.wdTable;  
                          object   which;  
                          which   =   Word.WdGoToDirection.wdGoToFirst;  
                          object   count;  
                          count   =   1;  
                          WordApp.Selection.GoTo(ref     what,   ref     which,   ref     count,   ref     missing);  
                          WordApp.Selection.Find.ClearFormatting();  
                          WordApp.Selection.Text   =   "";  
   
   
                  }  
                  public   void   GoToRightCell()  
                  {  
   
                          object   missing   =   System.Type.Missing;  
                          object   direction;  
                          direction   =   Word.WdUnits.wdCell;  
                          WordApp.Selection.MoveRight(ref     direction,   ref     missing,   ref     missing);  
                  }  
                  public   void   GoToLeftCell()  
                  {  
   
                          object   missing   =   System.Type.Missing;  
                          object   direction;  
                          direction   =   Word.WdUnits.wdCell;  
                          WordApp.Selection.MoveLeft(ref     direction,   ref     missing,   ref     missing);  
                  }  
                  public   void   GoToDownCell()  
                  {  
   
                          object   missing   =   System.Type.Missing;  
                          object   direction;  
                          direction   =   Word.WdUnits.wdLine;  
                          WordApp.Selection.MoveDown(ref     direction,   ref     missing,   ref     missing);  
                  }  
                  public   void   GoToUpCell()  
                  {  
   
                          object   missing   =   System.Type.Missing;  
                          object   direction;  
                          direction   =   Word.WdUnits.wdLine;  
                          WordApp.Selection.MoveUp(ref     direction,   ref     missing,   ref     missing);  
                  }  
                  public   void   InsertPageNumber(string   strType,   bool   bHeader)  
                  {  
                          object   missing   =   System.Type.Missing;  
                          object   alignment;  
                          object   bFirstPage   =   false;  
                          object   bF   =   true;  
                          switch   (strType)  
                          {  
                                  case   "Center":  
                                          alignment   =   Word.WdPageNumberAlignment.wdAlignPageNumberCenter;  
                                          //WordApp.Selection.HeaderFooter.PageNumbers.Item(1).Alignment   =   Word.WdPageNumberAlignment.wdAlignPageNumberCenter;  
                                          break;  
                                  case   "Right":  
                                          alignment   =   Word.WdPageNumberAlignment.wdAlignPageNumberRight;  
                                          //WordApp.Selection.HeaderFooter.PageNumbers.Item(1).Alignment   =   Word.WdPageNumberAlignment.wdAlignPageNumberRight;  
                                          break;  
                                  case   "Left":  
                                          alignment   =   Word.WdPageNumberAlignment.wdAlignPageNumberLeft;  
                                          WordApp.Selection.HeaderFooter.PageNumbers.Add(ref     alignment,   ref     bFirstPage);  
                                          break;  
                          }  
                  }  
   
                  //--------------------------------------------------------------------------------------------------------  
   
                  private   void   ReleaseAllRef(Object   obj)  
                  {//ReleaseComObject()方法可以使RCW减少一个对COM组件的引用,并返回减少一个引用后RCW对COM组件的剩余引用数量。  
                          //我们用一个循环,就可以让RCW将所有对COM组件的引用全部去掉。  
                          try  
                          {  
                                  while   (System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)   >   1)   ;  
                          }  
                          finally  
                          {  
                                  obj   =   null;  
                          }  
                  }  
   
                  //public   Word.Bookmark   GetBookmark(object   bookmarkName)  
                  //{    
                  ////WordApp.ActiveDocument.Bookmarks  
   
                  //}  
   
                  public   void   UpdateBookmark(Word.Bookmark   bookmark,   string   newText)  
                  {  
                          object   rng   =   bookmark.Range;  
                          string   bookmarkName   =   bookmark.Name;  
                          bookmark.Range.Text   =   newText;  
                          WordApp.ActiveDocument.Bookmarks.Add(bookmarkName,   ref   rng);  
                  }  
  public   void   UpdateBookmark(string   bookmarkName,   string   newText)  
                  {  
                          object   name   =   bookmarkName;  
                          Word.Range   rng   =   WordApp.ActiveDocument.Bookmarks.get_Item(ref   name).Range;  
                          rng.Text   =   newText;  
                          object   range   =   rng;  
                          WordApp.ActiveDocument.Bookmarks.Add(bookmarkName,   ref   range);  
                  }  
   
                  public   void   UpdateTableContent(int   tableID,   int   lineID,   int   columnID,   string   context)  
                  {//更改某表的某个单元格的内容  
                          Word.Table   tbl   =   WordApp.ActiveDocument.Tables[tableID];  
                          tbl.Cell(lineID,   columnID).Range.Text   =   context;  
                  }  
   
                  //将图片添加到文档中当前选定的位置  
                  //调用   InlineShapes   对象的   AddPicture   方法,并传入文件名。  
                  public   void   InsertPicture(string   bookmarkName,   string   pictureFileName)  
                  {  
                          object   name   =   bookmarkName;  
                          Word.Range   rng   =   WordApp.ActiveDocument.Bookmarks.get_Item(ref   name).Range;  
                          rng.InlineShapes.AddPicture(pictureFileName,  
                  ref   missing,   ref   missing,   ref   missing);  
                  }  
                  public   void   InsertPicture(string   pictureFileName)  
                  {  
                          WordApp.Selection.InlineShapes.AddPicture(pictureFileName,  
                  ref   missing,   ref   missing,   ref   missing);  
                  //         rng.InlineShapes.AddPicture(pictureFileName,  
                  //ref   missing,   ref   missing,   ref   missing);  
                  }  
   
                  public   void   DeleteBookmark(string   bookmarkName)  
                  {  
                          //删除书签  
                          //object   name   =   bookmarkName;  
                          //Word.Range   rng   =   WordApp.ActiveDocument.Bookmarks.get_Item(ref   name).Range;  
                          //object   wd   =   Word.WdUnits.wdCharacter;  
                          //rng.Delete(wd,   1);  
                  }  
   
                  public   void   DeleteText(string   text)  
                  {  
                          //删除查找到的文本  
                          object   findText   =   text;  
                          object   replaceWith   =   "";  
                          object   replaceAll   =   Word.WdReplace.wdReplaceAll;    
                          this.WordApp.Selection.Find.ClearFormatting();  
                          this.WordApp.Selection.Find.Replacement.ClearFormatting();    
                          this.WordApp.Selection.Find.Replacement.Text   =   "";    
                          this.WordApp.Selection.Find.Execute(ref   findText,  
                                    ref   missing,   ref   missing,   ref   missing,   ref   missing,   ref   missing,   ref   missing,  
                                    ref   missing,   ref   missing,   ref   replaceWith,   ref   replaceAll,   ref   missing,   ref   missing,  
                                    ref   missing,   ref   missing);  
   
                          object   unit   =   (int)Word.WdUnits.wdCharacter;  
                          object   count   =   1;  
                          this.WordApp.Selection.Delete(ref   unit,   ref   count);  
                  }  
   
   
   
   
                  public   int   CountPage()  
                  {  
                          //统计页数  
                          Word.WdStatistic   stat   =   Word.WdStatistic.wdStatisticPages;  
                          int   num   =   this.WordApp.ActiveDocument.ComputeStatistics(stat,   ref   missing);  
                          return   num;  
                  }  
   
          }  
  }