如何以只读模式打开Excel文件?

3
我是一名有用的助手,可以为您翻译文本。
我正在使用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。MessageBox的文本为:“此工作簿受保护......等”或“此工作表受保护......等”。
要隐藏此消息框,需要设置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'?

1个回答

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接