Home » AzureRSS

How to open an excel document in a webbrowser control

Hi,  I have an XBAP app that we would like to be able to open an Excel doc in a WebBrowser control but instead we always get prompted to opne in Excel.  Is this possible?  Here is what I have tried.  The reports are in SharePoint but we want to show them in our own application not SharePoint.

 

 

 

System.Windows.Forms.WebBrowser ReportView = new System.Windows.Forms.WebBrowser();

 

 

WindowsFormsHost ReportViewHost = newWindowsFormsHost();

ReportView.Navigate(_fileName,

 

"_self", postBytes, "Content-Type: application/vnd.m?s-excel");

ReportViewHost.Child = ReportView;

 

_filename would be something like http://localhost/shared%20documents/myfile.xslsx

Thanks for any suggestions.

Rhonda

 

5 Answers Found

 

Answer 1

Hi Rhonda,

It is possible to host the office application in the WPF application, please refer to the documents about Hosting a Microsoft Win32 Window in WPF: http://msdn.microsoft.com/en-us/library/ms742522.aspx#hosting_an_hwnd  and a sample provided by Dr. WPF may help you:http://drwpf.com/blog/2007/08/24/hosting-office-in-a-wpf-application/

I noticed you used a Windows.Forms.WebBrowser control and try to show the excel. Show an office document in the browser needs the ActiveX document object, please refer to this KB: http://support.microsoft.com/kb/243058. And the the problem is about how to integrate Windows Forms control in WPF, please refer to this document: http://msdn.microsoft.com/en-us/library/ms742474.aspx

(Note: if you develop a XBAP application, please use the full trust for it)

Sincerely,

Bob Bao

 

Answer 2

I've actually tried all those thing and still get prompted to open in excel.

 

Rhonda

 

Answer 3

Hi,

Thank you for your reply!

Could you please try to implement it in a desktop application first, then change it to the web application. I see it may have the aecurity problem in the web application. Did it work under the full-trust? If it is possible, could you please share some code about your application that we can help you to check?

Sincerely,

Bob Bao

 

Answer 4

Here are the two functions where I am trying to load the report.  I'll play around with a console app and see if I can get it going there although ultimately this needs to run in the Outlook Task Pane browser.

Thanks.

private System.Windows.Forms.WebBrowser ReportViewer = new System.Windows.Forms.WebBrowser();
private WindowsFormsHost ReportViewHost = new WindowsFormsHost();



privatevoid loadReport(string rptURL)
    {
      try
      {
        if ((rptUrl == "") || (rptUrl == null))
        {
          System.Windows.MessageBox.Show("We're sorry. We could not find a report with that Id.");
        }
        else
        {
          string sReport = getReportURL();
          
          cnvMain.Children.Remove(ReportViewHost);
          string postStr = "";
          byte[] postBytes = Encoding.UTF8.GetBytes(postStr);

          //ReportViewer.Navigate(sReport);
          ReportViewer.Navigate(rptURL);
          ReportViewHost.Child = ReportViewer;

          ReportViewHost.Width = int.Parse(defaultWidth);
          ReportViewHost.MinWidth = int.Parse(defaultWidth);

          ReportViewHost.Height = 600;
          ReportViewHost.MinHeight = 600;

          ReportViewHost.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
          ReportViewHost.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
          ReportViewHost.Visibility = Visibility.Visible;
          cnvMain.Children.Add(ReportViewHost);
        }
      }
      catch (Exception ex)
      {
        System.Windows.MessageBox.Show(ex.ToString());
      }
    }


privatevoid getPreferences(string listName, string reportName)
    {
      try
      {
        string sReport = getReportURL(); 
        
        //sample report URL//sReport = "http://server/sites/subsite/Shared Documents/excelfilename.xlsx";
        
        _fileName = new Uri(sReport);

        BasicHttpBinding httpBinding = new BasicHttpBinding();
        httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
        httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        httpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
        httpBinding.MaxBufferPoolSize = 2147483647;
        httpBinding.MaxReceivedMessageSize = 2147483647;
        httpBinding.MaxBufferSize = 2147483647;
        httpBinding.ReaderQuotas.MaxArrayLength = 2147483647;

        string excelServiceUrl = server + "/_vti_bin/ExcelService.asmx";

        EndpointAddress endpointAddress = new EndpointAddress(excelServiceUrl);

        this._file = new ExcelServiceSoapClient(httpBinding, endpointAddress);
        this._file.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
        _fileSessionId = _file.OpenWorkbook(_fileName.AbsoluteUri, string.Empty, string.Empty, out s);

        // _fileSessionId = _file.OpenWorkbookForEditing(_fileName.AbsoluteUri, string.Empty, string.Empty, out s);// byte[] workbook = _file.GetWorkbook(_fileSessionId, WorkbookType.FullWorkbook, out s);//// Write the resulting Excel file to stdout, as a binary stream.//BinaryWriter binaryWriter = new BinaryWriter(Console.OpenStandardOutput());//binaryWriter.Write(workbook);//binaryWriter.Close();string sCurrentParameterName = string.Empty;
        string sParameterValue = string.Empty;

        ParameterInfo[] p = new ParameterInfo[1];
        ParameterInfo param = new ParameterInfo();

        using (ClientContext clientContext = new ClientContext(server))
        {
          Web site = clientContext.Web;
          clientContext.Load(site);
          clientContext.ExecuteQuery();
          Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle(listName);

          CamlQuery camlQuery = new CamlQuery();
          camlQuery.ViewXml =
          @"<View><Query><Where><And><And><Eq><FieldRef Name='UserAlias'/><Value Type='Text'>" + app.User + "</Value></Eq><Eq><FieldRef Name='Title'/><Value Type='Text'>" + reportName + "</Value></Eq></And><Eq><FieldRef Name='SheetName'/><Value Type='Text'>SlicerValues</Value></Eq></And></Where></Query><RowLimit>100</RowLimit></View>";
          Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(camlQuery);
          clientContext.Load(list);
          clientContext.Load(listItems);
          clientContext.ExecuteQuery();
          if (listItems.Count > 0)
          {
            foreach (Microsoft.SharePoint.Client.ListItem listItem in listItems)
            {
              if (sCurrentParameterName != listItem["ParameterName"].ToString())
              {
                if (sCurrentParameterName != string.Empty)
                {
                  param.Name = sCurrentParameterName;
                  param.Values = a;
                  p[0] = param;
                  s = _file.SetParameters(_fileSessionId, p);
                }

                p = new ParameterInfo[1];
                param = new ParameterInfo();
                a = new ArrayOfString();

                sCurrentParameterName = listItem["ParameterName"].ToString();
                sParameterValue = listItem["ParameterValue"].ToString();
                a.Add(sParameterValue);
              }
              else
              {
                a.Add(listItem["ParameterValue"].ToString());
              }
            }
            s = _file.CloseWorkbook(_fileSessionId);

            //here it tries to open the report in Excel instead of in the browser
            loadReport(_fileName.AbsoluteUri);


            //This works but in order to open in excel services I need to use the url with the xlviewer and open the report that has the parameters set.
            loadReport(RptUrl);

            //RptUrl = "http://server/_layouts/xlviewer.aspx?id=/sites/subsite/Shared Documents/excelfile.xlsx";
          }
        }
      }
      catch (Exception ex)
      {
        System.Windows.MessageBox.Show(ex.ToString());
      }
    }

 

 

Answer 5

Hi

I had a similar problem

Tried lots of samples that worked but with another ie window.

Try this download :

http://download.cnet.com/Microsoft-Developer-Support-Office-Framer-Control-Sample-KB-311765/3000-2206_4-10732960.html

 

HTH

Guy

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter