I am trying to implement Export to excel functionality. I am passing my entire table in html format as string to the export function and trying to convert it into excel. Following is the code i have implemented
[HttpPost]
public void ExportToExcel(ExportDataEntity data)
{
StringWriter sw = new StringWriter();
sw.WriteLine(data.HtmlData);
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=test.xls");
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.Web.HttpContext.Current.Response.Charset = "";
byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
MemoryStream s = new MemoryStream(byteArray);
StreamReader sr = new StreamReader(s, Encoding.Unicode);
System.Web.HttpContext.Current.Response.Write(sr.ReadToEnd());
System.Web.HttpContext.Current.Response.End();
}
Following is my Entity class
public class ExportDataEntity
{
public string HtmlData { get; set; }
public string FileName { get; set; }
public string FileFormat { get; set; }
}
Following is the Table structure
Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SubCol21 | SubCol22 | SubCol23 | SubCol31 | SubCol32 | SubCol33 | SubCol51 | SubCol52 | SubCol53 | SubCol54 | SubCol61 | SubCol62 | SubCol63 | SubCol64 | SubCol65 | SubCol66 | ||
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
Count | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | |||||
Met % | 1 | 2 | 3 | 4 |
I am calling the ExportToExcel function via ajax call and passing entire html inside the div( consist only ). I am not getting any error in my code nor in my browser window.
I have googled alot and tried to implement various solution but nothing helped me.
Please do let me know what is the mistake I am doing, also provide the best solution to implement it in MVC.
Thanks