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

时间:2012-10-09 09:47:48

标签: c# wpf excel browser

我正在使用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"}

但是如果创建Application(不是从WebBrowserExcel.Document获取)我可以设置DisplayAlerts,但是我不能将Excel.Application托管到WebBrowser中,只能托管Excel的文件......

有没有办法在WebBrowser中只读Excel文件?或者如何设置'DisplayAlerts'?

1 个答案:

答案 0 :(得分:0)

相关问题