Answered by:
Not able to export to .xlsx.

Question
-
User908046561 posted
Hi,
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.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 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.
Regards,
Sartaj Husain
Friday, February 21, 2014 1:47 AM
Answers
-
User-1818759697 posted
Hi,
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:
http://www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-2007-2010-Report-in
http://www.up2technology.com/blog/export-to-excel-using-c/
Regards
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 23, 2014 9:13 PM
All replies
-
User-1818759697 posted
Hi,
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:
http://www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-2007-2010-Report-in
http://www.up2technology.com/blog/export-to-excel-using-c/
Regards
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 23, 2014 9:13 PM -
User908046561 posted
Thank You very Much.
Monday, February 24, 2014 9:17 AM