locked
Opening .xls files in Windows 10 RRS feed

  • Question

  • User1656897149 posted

    We are upgrading to Windows 10 and have begun testing all of our home page links. When we try to open a link in Excel in Windows 10 we are getting an error message that says the file type is not supported or may be corrupt. We can continue on past the error message and the file does come up. Is there something different we need to be doing so we are not seeing the message prior to the file opening in Excel? 

    Below is what we are using to pull up the file. (Still new at this ASP stuff so please bear with me!) :)

    <%Response.ContentType="application/vnd.ms-excel"%> <%Response.AddHeader "Content-Disposition", "inline; filename=output.xls"%>

    Thank you for any input!

    Wednesday, March 6, 2019 8:49 PM

All replies

  • User753101303 posted

    Hi,

    It looks like the error message shown by Microsoft Office when the file extension doesn't match the actual file format such as when you render HTML markup and pretend this to be an XLS file.

    Wednesday, March 6, 2019 9:04 PM
  • User-893317190 posted

    Hi jensohl94,

    It seems you are  outputting an excel in an aspx.

    When dealing with downloading file in aspx, you should pay attention that you may add extra content in  the output.

    For example, if your aspx is as follows.

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
         
            <asp:Button ID="Button1" runat="server" Text="download" OnClick="Button1_Click" />
     
        </form>
    </body>
    </html>

    When you write <%Response.ContentType="application/vnd.ms-excel"%> <%Response.AddHeader "Content-Disposition", "inline; filename=output.xls"%>,

    Html markup like form , body and other aspx's default script will also be mixed in your output , which may cause the wrong excel conent.

    Common solution is add Respone.Clear and Response.End to remove extra content.

    Below is sample code.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExcelisDestoriedProblem.aspx.cs" Inherits="WebFormCases2.officesample.ExcelisDestoriedProblem" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
         
            <asp:Button ID="Button1" runat="server" Text="download" OnClick="Button1_Click" />
    
                 <%
                
                 Response.Clear(); 
                Response.ContentType = "application/vnd.ms-excel";
    
                Response.AddHeader("Content-Disposition", "inline; filename=output.xls");
                Response.WriteFile(Server.MapPath("/officesample/My.xls"));   // output the file content
                Response.End();
                
                %>
          
        </form>
    </body>
    </html>

    Or you could write in code behind in button click event.

     public partial class ExcelisDestoriedProblem : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Clear();
                Response.ContentType = "application/vnd.ms-excel";
    
                Response.AddHeader("Content-Disposition", "inline; filename=output.xls");
                Response.WriteFile(Server.MapPath("/officesample/My.xls"));
                Response.End();
            }
        }

    Best regards,

    Ackerly Xu

    Thursday, March 7, 2019 2:38 AM