黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

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

系統 3462 0
原文: C#操作Word文檔(加密、解密、對應書簽插入分頁符)

最近做一個項目,客戶要求對已經生成好的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 = 
      
        "
      
      
        ITIS@997168
      
      
        "
      
      
        ;


      
      
         9
      
      
        object
      
       PassWord =
      
         Pass;


      
      
        10
      
                   MSWord.Application wordApp;  
      
        //
      
      
        Word應用程序變量
      
      
        11
      
                   MSWord.Document wordDoc;    
      
        //
      
      
        Word文檔變量
      
      
        12
      
      
        try
      
      
        13
      
      
                    {


      
      
        14
      
      
        object
      
       Nothing = Missing.Value;  
      
        //
      
      
        初始化
      
      
        15
      
                       wordApp = 
      
        new
      
      
         MSWord.ApplicationClass();


      
      
        16
      
      
        17
      
      
        //
      
      
         打開已存在的Word
      
      
        18
      
      
        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
      
      
        //
      
      
        定義書簽名稱  PartB
      
      
        45
      
      
        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
      
      
        //
      
      
        定義書簽名稱  PartC1
      
      
        51
      
      
        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
      
      
        //
      
      
        定義書簽名稱  PartC2
      
      
        57
      
      
        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
      
       }
    

?

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論