2

How can I export an .xlsx file to excel through mvc using chrome. It works for .xls but not .xlsx

Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename= Estimate1.xlsx");
    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    Response.Write(sw.ToString());
    Response.End();

and when i open excel file. it show like this. "Excel cannot open file 'FileName.xlsx' because the file format or file extension is not valid."

so much appreciated your help! :)

Rabbit
  • 21
  • 1
  • 3
  • possible duplicate of [What is a correct mime type for docx, pptx etc?](http://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc) – Lorenzo Dematté Jan 09 '14 at 10:06
  • sorry if i late to say i using MVC4 and when i rendercontrol : grid.RenderControl(htw); Excel file cant open because file have HTML Tag is in :( – Rabbit Jan 09 '14 at 10:11
  • @LorenzoDematté I wouldn't say that is a duplicate. But it does contain the answer – albertjan Jan 09 '14 at 18:59

2 Answers2

3

Try changing content type for .xlsx

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
0

You should use the correct MIME type. For DOCX is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

See this blog post and this answer for more details

Community
  • 1
  • 1
Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77