C#操作Word文檔(加密、解密、對應書籤插入分頁符)

來源:互聯網
上載者:User

標籤:style   class   blog   code   tar   ext   

最近做一個項目,客戶要求對已經產生好的RTF檔案中的內容進行分頁顯示,由於之前對這方面沒有什麼瞭解,後來在網上也找了相關的資料,並結合自己在MSDN上面的尋找,後來總算把問題給解決掉啦。下面對C#操作Word文檔(加密、解密、插入分頁符)做一個簡單的總結,希望對一些朋友有所幫忙吧。^_^

寫代碼之前,需要引用對應的DLL檔案:

1、Interop.Microsoft.Office.Interop.Word.dll  (網上可以下載)

2、mscorlib.dll  (添加引用--->.NET中即可找到)

 1 using Microsoft.Office.Interop.Word; 2 using MSWord = Microsoft.Office.Interop.Word; 3 using System.Reflection; 4  5 private void button1_Click(object sender, System.EventArgs e) 6  { 7             //Word文檔保護密碼 8             string Pass = "[email protected]"; 9             object PassWord = Pass;10             MSWord.Application wordApp;  //Word應用程式變數11             MSWord.Document wordDoc;    //Word文檔變數12             try13             {14                 object Nothing = Missing.Value;  //初始化15                 wordApp = new MSWord.ApplicationClass();16 17                 // 開啟已存在的Word18                 object FileName = @"E:\archive\CMPLatest_2117_230614-1053.Rtf";19                 object readOnly = false;20                 object isVisible = true;21                 object objFalse = false;22 23                 wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly, ref Nothing, ref PassWord, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);24 25                 //啟用Word文檔26                 wordDoc.Activate();27                 //判斷是否有密碼28                 if (wordDoc.HasPassword)29                 {30                     wordDoc.Password = null;31                 }32 33                 //檢查是否為Word文檔設定保護功能,沒有設定保護功能,就解除密碼保護34                 if (wordDoc.ProtectionType != WdProtectionType.wdNoProtection)35                 {36                     wordDoc.Unprotect(ref PassWord);37                 }38 39                 //跳轉到指定書籤40                 object toMark = MSWord.WdGoToItem.wdGoToBookmark;41                 //分頁符42                 object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;  43 44                 //定義書籤名稱  PartB45                 object BookMarkName_b = "bmf_b";46                 wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_b);47                 //插入分頁符48                 wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);49                 50                 //定義書籤名稱  PartC151                 object BookMarkName_c1 = "bmf_c1";52                 wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c1);53                 //插入分頁符54                 wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);55 56                 //定義書籤名稱  PartC257                 object BookMarkName_c2 = "bmf_c2";58                 wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c2);59                 //插入分頁符60                 wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);61 62                 //對Word文檔進行加密保護63                 if(PassWord.ToString() != null)64                 {65                     wordDoc.Protect(WdProtectionType.wdAllowOnlyReading, ref objFalse, ref PassWord, ref Nothing, ref Nothing);66                 }67                 68 69                 //將插入分頁符後的Word文檔儲存一下70                 wordDoc.SaveAs(ref FileName, ref Nothing, ref Nothing, ref Nothing, ref objFalse, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);71 72                 //標記為最終狀態,禁止彈出對話方塊73                 //wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;74                 //標記為最終狀態75                 //wordDoc.Final = true;76 77                 //關閉Word文檔78                 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);79                 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);80             }81             catch(Exception ex)82             {83                 84             }85 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.