0

I want to create an .xlsx file not .xls using my razor view string, But it doesn't work here is my code

private ContentResult GenerateExcel<T>(string viewName, T model) where T : class
    {
        var htmlViewRenderer = new HtmlViewRenderer();
        string htmlText = htmlViewRenderer.RenderViewToString(this, viewName, model);
        //  byte[] renderedBuffer;
        Response.ClearContent();
        Response.Buffer = true;

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

        Response.Output.Write(htmlText.ToString());
        Response.Flush();
        Response.End();
       return  Content(htmlText,Response.ContentType);

    }
Rutu
  • 125
  • 2
  • 10
  • possible duplicate of [What is correct content-type for excel files?](http://stackoverflow.com/questions/2937465/what-is-correct-content-type-for-excel-files) – Pedro.The.Kid Sep 24 '14 at 09:57
  • @Pedro.The.Kid thanks but I already tried this but this does not work with XLSX format when i explicitly change extension for xlsx to xls it work – Rutu Sep 24 '14 at 10:27
  • it states that for XLSX the content type is different – Pedro.The.Kid Sep 24 '14 at 12:47

1 Answers1

0

IText-sharp comes with nuget package it helped to create excel i use it and it don't have any issue for xls or xlsx format

Rutu
  • 125
  • 2
  • 10