I would like to export data to a .xlsx
file, but I can only seem to export to .xls
.
What's the easiest way to export a .xlsx
file?
This is the code I use to export to a .xls
file:
GridView gv = new GridView();
gv.DataSource = listCatalogue.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Catalogue.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
You can use an external library like EPPlus.
Taken from the EPPlus documentation: