How to open Excel file with Read Only protection?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 11:51:55

问题


I have opened Excel file in my C# WinForm Application adding reference to Microsoft.Office.Interop.Excel.dll and using DSO FRAMER CONTROL. But i want to open my excel file with read only protection.I have successfully done this for WORD Application like this

Word.Document wordDoc = (Word.Document)axFramerControl1.ActiveDocument;
Word.Application wordApp = wordDoc.Application;
wordDoc.Protect(Word.WdProtectionType.wdAllowOnlyReading);

In the same i want to do this work for Excel.But i couldn't able to protect Excel file on that way.

string path = "C:\\test-wb.xlsx";
axFramerControl1.Open(path, true,"excel.sheet", "", "");

Excel._Workbook excelDoc   =(Microsoft.Office.Interop.Excel._Workbook)axFramerControl1.ActiveDocument;
Excel.Application excelApp =excelDoc.Application;
//What code should i write to protect Excel Workbook with read - only.
excelDoc.Protect(misval, true, misval);//It is not working.

回答1:


Call theOpen method with third parameter (ReadOnly) = true.

See MSDN documentation :

ReadOnly
Optional Object. True to open the workbook in read-only mode.




回答2:


The WorkBook class has a Protect method, similar (but not identical) to the one supported by Word. I can't find the COM/interop documentation, but the VSTO documentation covers the same ground, and the method signatures are the same:

Protect

Protects a workbook so that it cannot be modified.

public virtual void Protect (
    [OptionalAttribute] Object Password,
    [OptionalAttribute] Object Structure,
    [OptionalAttribute] Object Windows
)

(This all assumes that what you wanted to achieve was to protect the document, since that's what the Word code does, as opposed to opening the document read only, which is what your narrative seems to be saying, in which case, @gdoron's answer is more suitable



来源:https://stackoverflow.com/questions/8906670/how-to-open-excel-file-with-read-only-protection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!