work,excel ,ppt 只读

9 篇文章 0 订阅

 //***************** word 只读 ****************//

            try
            {
                object obj1 = true;
                object obj2 = "aaaaasdfa";
                object obj3 = false;
                object obj4 = true;
                object obj5 = "123";
                object missing = System.Reflection.Missing.Value;
                object readOnly = true;
                object obj = @"F:/复件 工程概况.doc";
                Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
                object objin =-1;
                Microsoft.Office.Interop.Word._Document wodoc = word.DocumentsOpen(ref obj, 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);
                wodoc.Unprotect(ref obj5); //如果原来的有密码,要先解开
                wodoc.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyFormFields, ref obj1, ref obj2, ref obj3, ref obj4);
                wodoc.Save();
            }
            catch (Exception ex)
            {

            }
            ************* word end  ************//


            try
            {
                ***************** excel 只读 ****************//
                object missing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Excel.Workbook wcc = null;
                Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Microsoft.Office.Interop.Excel.Worksheet excelsheet = null;
                wcc = excel.Workbooks.Open("F://aaa.xls", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                wcc = (Microsoft.Office.Interop.Excel.Workbook)this.axFramerControl1.ActiveDocument;
                object obj = "asdfasdfa";

                wcc.WritePassword = "asdfa"; //1 : 只要设置此属性就可以使文件成为只读的,但是用户用只读方式打开,还可以编辑,保存副本。

                wcc.Protect(obj, true, true); //2 :此属性设置工作薄结构不能修改,也就是不能增加,删除工作表,但是还可以编辑,保存副本
                int i = wcc.Worksheets.Count;
                3 : 可以循环工作表worksheet 来设置每个工作表的状态,主要是protect 后面的bool值。这些属性对应的可以查看(自己打开excel ->右键点击底下的工作表名--->选择保护工作表 就可以看到了)
                excelsheet = (Microsoft.Office.Interop.Excel.Worksheet)wcc.Sheets.get_Item(1);

                excelsheet.Protect("asfdasdf", missing, missing, missing, true, false, false, false, false, false, false, false, false, false, false, false);
                excelsheet = (Microsoft.Office.Interop.Excel.Worksheet)wcc.Sheets.get_Item(2);

                excelsheet.Protect("asfdasdf", missing, missing, missing, true, false, false, false, false, false, false, false, false, false, false, false);
                excelsheet = (Microsoft.Office.Interop.Excel.Worksheet)wcc.Sheets.get_Item(3);

                excelsheet.Protect("asfdasdf", missing, missing, missing, true, false, false, false, false, false, false, false, false, false, false, false);
                wcc.Save();
                ********************************************//
            }
            catch (Exception ex)
            {

            }

 

//找不到powerpoint的protect方法,只能用writepassword来设置,知道的朋友告知一下,谢谢!!

//***************** ppt 只读 ****************//
            try
            {
                //this.axFramerControl1.Open("F://aaa.xls");
                Microsoft.Office.Interop.PowerPoint.ApplicationClass ppt = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                Microsoft.Office.Interop.PowerPoint.Presentations pres = null;
                Microsoft.Office.Interop.PowerPoint.Presentation pre = null;
                pre = ppt.Presentations.Open("F://bb.ppt", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                pre.WritePassword = "asdf";
                pre.Save();
            }
            catch (Exception ex)
            {
           
            }
            //********************************************//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值