//***************** 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)
{
}
//********************************************//