Last post Feb 20, 2014 10:28 AM by sartajh
Nov 12, 2012 10:02 AM|Naresh Polu|LINK
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.
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.
Nov 14, 2012 03:01 AM|Dalong Zhang - MSFT|LINK
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.
Feb 20, 2014 10:28 AM|sartajh|LINK
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.AddHeader("content-disposition", "attachment;filename=" + "fileName.xls");
curContext.Response.Charset = "";
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;
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.