【发布时间】:2012-09-29 14:52:27
【问题描述】:
我正在使用 VS2008、C# 和 WPF 应用程序(WebBrowser 和 Microsoft.Office.Interop.Excel) 我有一个任务:在 WPF 表单上以只读模式托管 Excel。
我该怎么做:
首先1.在WebBrowser中加载Excel文件
Uri _uri = new Uri("FilePath");
WebBrowserExcel.Navigate(_uri);
下一步 2. 在 WebBrowserExcel_LoadCompleted 事件中
if ((WebBrowserExcel.Document as Excel.Workbook) == null) return;
try
{
//Get loading WorkBook and set "Protect" for disable editing
_Book = WebBrowserExcel.Document as Excel.Workbook;
_Book.Protect("", Type.Missing, Type.Missing);
//Set "Protect" for disable editing to WorkSheet
foreach (Excel.Worksheet _sheet in _Book.Sheets)
{
_sheet.Protect("", Type.Missing,Type.Missing,
Type.Missing,Type.Missing, true,Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,Type.Missing,
Type.Missing, Type.Missing, Type.Missing
);
}
}
.....
之后,当我尝试在 WebBrowser 中编辑文件时,我有一个 Excel 的 MessageBox。消息框的文本: “This Book is Protected...etc”或“This Sheet is Protected...etc”
要隐藏此消息框,我需要设置属性 Excel.Application.DisplayAlerts = false;
3. Set property
_Book.Application.GetType().InvokeMember("DisplayAlerts", BindingFlags.SetProperty, null, _Book.Application, new object[] { false });
并且有例外:
InnerException = {"could Not set the property DisplayAlerts Application class"}
但如果创建应用程序(不是从 WebBrowserExcel.Document 获取)我可以设置 DisplayAlerts,但我不能将 Excel.Application 托管到 WebBrowser,只有 Excel 的文件...
有没有办法在 WebBrowser 中将 Excel 的文件设置为只读?或者如何设置“DisplayAlerts”?
【问题讨论】: