mvc html转excel,jquery - MVC Export to Excel from HTML Table in div - Stack Overflow

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

Col1Col2Col3Col4Col5Col6
SubCol21SubCol22SubCol23SubCol31SubCol32SubCol33SubCol51SubCol52SubCol53SubCol54SubCol61SubCol62SubCol63SubCol64SubCol65SubCol66
123456789101112131415161718
Count123456789101112
Met %1234

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值