locked
Response.Contenttype for Excel 2007 or 2010 (.xlsx) format is not working RRS feed

  • Question

  • User-1668203914 posted

    Hi,

    Currently all our excel sheets are being generated in Excel (1997-2003) format which is of extension .xls. These types of excel will have a problem when user have higher version of office installed. (Due to Extension Hardening Feature : A warning messages will be dispalyed before opening the excel sheet). As per our requirment we need to generate excel sheets in .xlsx format.

    Code :

    Case "EXCEL"       

                                    Response.AppendHeader("Content-Disposition","filename=" test&".xlsx")                                                                  Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

    I have used the correct MIME type for Excel 2007/2010 as mentioned above.

    Excel file which is being generated from web site is opening in .xlsx type but i am getting below error message :

    Excel cannot open the file .xlsx because the file format or file extension is not valid.Please make sure that fiel is not corrupted. 

    But when I tried to save the same file in .xls format, it is opening fine without any issues.

    My web site is hosted in IIS 7, so b ydefault all MIME types are already added.

    Can any one please help me in resolving the issue.

    Thanks,

    Naresh

     

     

     

     

     

     

     

    Monday, November 12, 2012 10:02 AM

All replies

  • User1048621291 posted

    Hi,

    Response.AppendHeader("Content-Disposition","filename=" test&".xlsx") 

    We can change any file extension to .xlsx or anything we want. OS automatically will change the icon for the file to the  related program icon, so we see on surface is an xlsx file with Excel icon , but actually , the file itself is not an xlsx file.

    Wednesday, November 14, 2012 3:01 AM
  • User908046561 posted

    Hi Naresh,

    Did you get above mentioned code working?  I need this. I am doing it with MVC5. Exporting to .XLS is fine but result in warning message while opening .xls file in Excel 2013. 

    Your help is highly appreciated.

    Below is my code.

    HttpContext curContext = HttpContext.Current;
                curContext.Response.Clear();
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + "fileName.xls");
                curContext.Response.Charset = "";
                curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                curContext.Response.ContentType = "application/ms-excel";

                //Open a memory stream that you can use to write back to the response
                byte[] byteArray = Encoding.ASCII.GetBytes(ExcelGridView);
                MemoryStream s = new MemoryStream(byteArray);
                StreamReader sr = new StreamReader(s, Encoding.Unicode);

                //Write the stream back to the response
                curContext.Response.ContentEncoding = System.Text.Encoding.Unicode;
                curContext.Response.Write(sr.ReadToEnd());
                curContext.Response.End();

    I have already tried with "curContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";" but no success. I want to suppress warning message. Above code is not working if i set the extention .xlsx, nothing is appearing in EXCEL file and also warning message is persist.

    Regards,

    Sartaj Husain

    Thursday, February 20, 2014 10:28 AM
  • User1395454797 posted

    add following

    Dim xSaveOptions As New XlsSaveOptions(SaveFormat.SpreadsheetML)

    Dim tempStream As New IO.MemoryStream()

    wb.Save(tempStream, xSaveOptions)

    Response.Clear()
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Response.AppendHeader("Content-Disposition", "attachment; filename=test.xlsx")
    Response.BinaryWrite(tempStream.ToArray())

    Friday, June 11, 2021 1:23 PM