Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Display Excel File in .aspx Page

Display Excel File in .aspx Page

Scheduled Pinned Locked Moved ASP.NET
csharphtmlsysadminhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paas
    wrote on last edited by
    #1

    Hello, I am trying to display an Excel file in a .aspx page that is opened from the page load event of another .aspx page via a window.open. The HTML of the .aspx page that will display the Excel is as follows...

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExcelViewer.aspx.cs" Inherits="ExcelViewer"%>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Excel Viewer</title>
    </head>
    <body>
    <form id="form1" runat="server">

    </form>
    

    </body>
    </html>

    The PageLoad event of the code behind the .aspx page that will display the Excel file includes the following code:

       FileInfo fi = new FileInfo(sFilePath);  // sFilePath is path to Excel file...
    
       Response.Clear();
       Response.ClearHeaders();
       Response.ClearContent();
       Response.ContentType = sMIME;  // sMIME stores the mime type
       Response.AddHeader("Content-Disposition", "inline; filename=" + fi.Name);
    
        Response.WriteFile(sFilePath);  
        Response.Flush();
    

    HttpContext.Current.ApplicationInstance.CompleteRequest();
    File.Delete(sFilePath);

    What I am finding is that the first time I try to open the Excel file with this code, I get the 'Internet Explorer cannot display the web page...Diagnose connections problems' error page. Then if I try to view the page shortly thereafter the Excel file comes up in the page just fine. Then if I try to view the page a 3rd time I get the error page again...etc. So every other time the viewing is succeeding. Does anyone have any thoughts as to why this is only succeeding in viewing the Excel file every other time? Edit: I have found that if my Content-Disposition is set to 'attachment', after receiving the 'Open' or 'Save' prompt and selecting 'Open' the file does open correctly every time in Excel. I would still prefer to use a disposition of 'inline' so the user does not have to be prompted each time a file is to be viewed. Thank You.

    R 1 Reply Last reply
    0
    • P paas

      Hello, I am trying to display an Excel file in a .aspx page that is opened from the page load event of another .aspx page via a window.open. The HTML of the .aspx page that will display the Excel is as follows...

      <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExcelViewer.aspx.cs" Inherits="ExcelViewer"%>

      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
      <title>Excel Viewer</title>
      </head>
      <body>
      <form id="form1" runat="server">

      </form>
      

      </body>
      </html>

      The PageLoad event of the code behind the .aspx page that will display the Excel file includes the following code:

         FileInfo fi = new FileInfo(sFilePath);  // sFilePath is path to Excel file...
      
         Response.Clear();
         Response.ClearHeaders();
         Response.ClearContent();
         Response.ContentType = sMIME;  // sMIME stores the mime type
         Response.AddHeader("Content-Disposition", "inline; filename=" + fi.Name);
      
          Response.WriteFile(sFilePath);  
          Response.Flush();
      

      HttpContext.Current.ApplicationInstance.CompleteRequest();
      File.Delete(sFilePath);

      What I am finding is that the first time I try to open the Excel file with this code, I get the 'Internet Explorer cannot display the web page...Diagnose connections problems' error page. Then if I try to view the page shortly thereafter the Excel file comes up in the page just fine. Then if I try to view the page a 3rd time I get the error page again...etc. So every other time the viewing is succeeding. Does anyone have any thoughts as to why this is only succeeding in viewing the Excel file every other time? Edit: I have found that if my Content-Disposition is set to 'attachment', after receiving the 'Open' or 'Save' prompt and selecting 'Open' the file does open correctly every time in Excel. I would still prefer to use a disposition of 'inline' so the user does not have to be prompted each time a file is to be viewed. Thank You.

      R Offline
      R Offline
      Ravi Sant
      wrote on last edited by
      #2

      I am not sure, but something like this has worked fine for me: Response.AddHeader("content-disposition", "attachment; filename=GExcel.xls"); It could be case case sensitive.

      // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

      Tell your manager, while you code: "good, cheap or fast: pick two. "

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups