如何打开只读保护的Excel文件?
我已在 C# WinForm 应用程序中打开 Excel 文件,添加对 Microsoft.Office.Interop.Excel.dll 的引用并使用 DSO FRAMER CONTROL。但我想以只读保护打开我的 Excel 文件。我已经成功地为 WORD 应用程序完成了此操作,如下所示:
Word.Document wordDoc = (Word.Document)axFramerControl1.ActiveDocument;
Word.Application wordApp = wordDoc.Application;
wordDoc.Protect(Word.WdProtectionType.wdAllowOnlyReading);
以同样的方式,我想为 Excel 执行此工作,但我无法以这种方式保护 Excel 文件。
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.
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 manner, I want to do this work for Excel, but I couldn't protect the Excel file 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用第三个参数 (
ReadOnly
) =true
调用Open
方法。请参阅 MSDN 文档:
只读
可选对象。如果为 True,则以只读模式打开工作簿。
Call the
Open
method with third parameter (ReadOnly
) =true
.See MSDN documentation :
ReadOnly
Optional Object. True to open the workbook in read-only mode.
WorkBook 类有一个 Protect 方法,与 Word 支持的方法类似(但不相同)。我找不到 COM/interop 文档,但 VSTO 文档涵盖相同的基础,并且方法签名相同:
保护
(这一切都假设您想要实现的是保护文档,因为这就是 Word 代码的作用,而不是以只读方式打开文档,这就是您的叙述似乎所说的,在这种情况下, @gdoron的答案更合适
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
(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