导出HTML表到MS Excel格式使用jQuery [英] Export HTML table to MS Excel Format Using jQuery

查看:127
本文介绍了导出HTML表到MS Excel格式使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照这个例如。要使用jQuery的HTML表导出到MS Excel格式。

I'm trying to follow this example. To export an HTML table to MS Excel format using jQuery.

下面是我的.aspx:

Here's my .aspx:

<head runat="server">
    <title></title>
    <script src="Scripts/jQuery.1.8.3.js" type="text/javascript"></script>
    <script src="Scripts/JScript2.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="tbl" runat="server">
        </asp:Table>
        <input type="button" id="btnExport" value=" Export Table data into Excel " />
    </div>
    </form>
</body>

的.js文件(JScript2.js):

The .js (JScript2.js):

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

...和codebehind:

... And the codebehind:

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);    
    }
}

生成的页面源代码:

Generated page source:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title>
    <script src="Scripts/jQuery.1.8.3.js" type="text/javascript"></script>
    <script src="Scripts/JScript2.js" type="text/javascript"></script>
</head>
<body>
    <form method="post" action="JQuery_Export_To_Excel.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2NTA0OTEwODdkZKr9kRtjn1C5sAo2woCwfF/8uHOVcyNi1bu4OtVBNKlS" />
</div>

<div class="aspNetHidden">

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAIdFUHUqRwMmhFieKB52uC8avDSflAS9b8PVcR1BxTTBqeDRyg6lH5NKPWh6Jt5ee2zX+bYNkguHBdZjCzKvoJa" />
</div>
    <div>
        <table id="tbl">
    <tr>
        <td>AAA</td><td>BBB</td>
    </tr>
</table>
        <input name="btnExport" type="button" id="btnExport" value=" Export Table data into Excel " />
    </div>
    </form>
</body>
</html>

我不知道我做错了,但是当我点击的 btnExport 的,没有任何反应。我怎样才能解决这个问题?

I don't know what I'm doing wrong, but when I click btnExport, nothing happens. How can I fix this problem?

推荐答案

把这个表单

<div>
    <div id="container">
        <asp:Table ID="tbl" runat="server">
        </asp:Table>
    </div>
    <input type="button" id="btnExport" value=" Export Table data into Excel " />
</div>

在脚本标记头节

将这个

Put this in the header section with script tag

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

这篇关于导出HTML表到MS Excel格式使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆