C#操縱Word

首先添加引用,解決方案資源管理器-》引用-》添加-》Com-》瀏覽-》C:/Program Files/Microsoft Office/OFFICE11/MSWORD.OLB    我使用的是office 2003其他版本我不太清楚,.net會自動把OLB控件轉換成DLL文件

使用方法:

  object oMissing = System.Reflection.Missing.Value;

   Word.Application oWord =new Word.Application();

  oWord.Visible = false;//設置Word應用程序爲不可見

//新建一個Word文檔
   Word.Document oDoc=oWord.Documents.Add(ref oMissing,ref oMissing ,ref oMissing,ref oMissing);   

//文檔內容的複製與粘貼

    oDoc.Content.Copy();
    oDoc.Content.Paste()

//文檔的另存爲

oDoc.SaveAs(ref fileName,ref saveFormat,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);

//其他設置

   oDoc.PageSetup.PaperSize=Word.WdPaperSize.wdPaperA3;//頁面設置
   oDoc.PageSetup.Orientation=Word.WdOrientation.wdOrientLandscape;//橫板還是豎板
   oDoc.PageSetup.TextColumns.SetCount(2);//分欄

//關閉Word

   oWord.Application.Quit(ref b,ref oMissing,ref oMissing);
   System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);

通過oDoc對象對Word文檔進行操作(word能做的它都能做)進行操作裏面有很多函數,有興趣的自己研究

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章