使用jQuery将HTML表导出为MS Excel格式

我试着效仿这个例子 。 使用jQuery将HTML表导出为MS Excel格式。

这是我的.aspx:

      

.js(JScript2.js):

 $("#btnExport").click(function (e) { window.open('data:application/vnd.ms-excel,' + $('#tbl').html()); e.preventDefault(); }); 

……以及代码隐藏:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class JQuery_Export_To_Excel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TableRow tr = new TableRow(); TableCell tc = new TableCell(); tc.Text = "AAA"; tr.Cells.Add(tc); tc = new TableCell(); tc.Text = "BBB"; tr.Cells.Add(tc); tbl.Rows.Add(tr); } } 

生成的页面源:

         
AAABBB

我不知道我做错了什么,但是当我点击btnExport时 ,没有任何反应。 我该如何解决这个问题?

把它放在你的表格中

 

将其放在带有脚本标记的标题部分中

  $(document).ready(function () { $("#btnExport").click(function (e) { window.open('data:application/vnd.ms-excel,' + $('#container').html()); e.preventDefault(); }); }); 

我相信您的代码没有任何问题,因为您的代码是asp.net。 你不能直接使用$(’#tbl’)。 你必须使用$(“#”+’<%= tbl.ClientID%>‘)