C# 如何打开具有只读保护的 Excel 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8906670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open Excel file with Read Only protection?
提问by Saravanan
I have opened Excel file in my C# WinForm Application adding reference to Microsoft.Office.Interop.Excel.dlland 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
我已经在我的 C# WinForm 应用程序中打开了 Excel 文件,添加了对Microsoft.Office.Interop.Excel.dllDSO FRAMER CONTROL 的引用和使用。但我想用只读保护打开我的 excel 文件。我已经成功地为这样的 WORD 应用程序做到了这一点
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.
同样,我想为 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.
采纳答案by gdoron is supporting Monica
Call theOpenmethod with third parameter (ReadOnly) = true.
调用Open带有第三个参数 ( ReadOnly) = 的方法true。
See MSDN documentation:
请参阅 MSDN文档:
ReadOnly
Optional Object. True to open the workbook in read-only mode.
只读
可选对象。True 以只读模式打开工作簿。
回答by Damien_The_Unbeliever
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:
WorkBook 类有一个 Protect 方法,与 Word 支持的方法类似(但不完全相同)。我找不到 COM/interop 文档,但 VSTO 文档涵盖了相同的内容,并且方法签名是相同的:
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 answeris more suitable
(这一切都假设您想要实现的是保护文档,因为这就是 Word 代码所做的,而不是以只读方式打开文档,这就是您的叙述似乎在说的内容,在这种情况下,@gdoron 的回答更合适

