Display Excel File in .aspx Page
-
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.
-
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.
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. "