Last post Feb 24, 2014 09:17 AM by sartajh
Feb 21, 2014 01:47 AM|sartajh|LINK
I am trying to export the date in MVC5. Exporting to .XLS is fine but result in warning message while opening .xls file in Excel 2013. I want to export to .xlsx file and suppress the warning message.
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 which is appareaing while trying to .xls file in Excel 2013. Above code
is not working if i set the extention .xlsx, nothing is appearing in EXCEL file and also warning message is persist.
Feb 23, 2014 09:13 PM|Shawn - MSFT|LINK
Excel 2003 could open that format(.xls), however, the file was not in Excel format. XLSX is microsoft open xml format and it is actually a compressed file that contains the XML and styles in seperate files. The above codes can still work for Excel 2007/2010
or above, just leave the extension as .xls and Excel can open it, but there would be a warning message pop up.
For this situation, you could use extended libraries which I recommend using EPPlus which is a .net library that reads & writes Excel 2007/2010 files using the Open Office Xml format (xlsx).
For more information, you could refer to the following links:
Feb 24, 2014 09:17 AM|sartajh|LINK
Thank You very Much.