locked
Not able to export to .xlsx. RRS feed

  • 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

All replies