我正在阅读这篇博客 http://growingtech.blogspot.in/2012/10/export-html-to-excel-using-jquery-and.html
aspx页面如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="exportPage.aspx.cs" EnableEventValidation="false" Inherits="exportPage" %>
<!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 runat="server">
<title></title>
<script src="../../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function exportexcel() {
var data = $("#toReport").html();
data = escape(data);
$('body').prepend("<form method='post' action='exportPage.aspx' style='top:-3333333333px;' id='tempForm'><input type='hidden' name='data' value='" + data + "' ></form>");
$('#tempForm').submit();
$("tempForm").remove();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Export
</h2>
<asp:Button ID="Button1" runat="server" Text="Export" OnClientClick="exportexcel()" onclick="Button1_Click" />
<div id="toReport">
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Email</th>
</tr>
<tr>
<td>John</td>
<td>44</td>
<td>john@gmail.com</td>
</tr>
<tr>
<td>Rambo</td>
<td>33</td>
<td>rambo@gmail.com</td>
</tr>
<tr>
<td>It's hot</td>
<td>33</td>
<td>test@hotmail.com</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
和c#低于
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class exportPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string data = Request.Form["data"];
data = HttpUtility.UrlDecode(data);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=report.xls");
Response.Charset = "";
Response.ContentType = "application/excel";
HttpContext.Current.Response.Write(data);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
我想知道我在哪里没有将表数据传递给excel?我的jquery链接似乎引用了ok,但somwhow我最终下载了一个空的excel。谢谢你的建议!
答案 0 :(得分:0)
我认为问题在于您使用的是服务器端按钮,并且在提交代码之前调用了JavaScript函数,但由于这两个操作都是异步运行,因此无法保证在将表单发布到表单之前填充表单数据。服务器
此外,您应该知道提交:form method='post' action='exportPage.aspx'
会点击exportPage.aspx
,因此会引发其PageLoad,但按钮( Button1_Click)
的处理程序不会被点击。