用ajax调用以response输出到客户端导出excel不成功,不报错也不提示下载 5
大神们,帮帮忙!(由于项目需要必须用ajax调用后台方法,不能用服务器端控件)ajax调用了后台代码,后台代码全部执行完毕,不报错,但是就是不提示下载。我的代码如下:<i...
大神们,帮帮忙!(由于项目需要必须用ajax调用后台方法,不能用服务器端控件)
ajax调用了后台代码,后台代码全部执行完毕,不报错,但是就是不提示下载。
我的代码如下:
<input type="button" value="导出到Excel" onclick="ExportToExcel2()" />
<script type="text/javascript">
function ExportToExcel2() {
$.ajax({
type: "post",
url: "welcome.aspx?action=export2&time=" + Math.random,
success: function (data) {
alert('导出到Excel成功');
},
error: function () {
alert('导出到Excel失败!');
}
});
}
</script>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["action"] == "export2")
{
DataTable dt = Session["exportTable"] as DataTable;
ExportToExcel2(dt);
}
}
public void ExportToExcel2(DataTable dt)
{
System.Web.UI.WebControls.GridView gridview = new System.Web.UI.WebControls.GridView();
//创建DataGrid控件,并把要到处的datatable数据绑定到DataGrid控件上(导出DataGrid上的值就是导出需要的datatable)
gridview.ID = "excelGrid1";
gridview.DataSource = dt;
gridview.DataBind();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition","attachment;filename=盘存表明细统计.xls");
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.Default;
gridview.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gridview.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
我解决了,呵呵,这里分享一下,可以在按钮事件中跳转window.open("test_export.aspx");
然后在test_export.aspx.cs页面的load事件中写导出为excel代码 展开
ajax调用了后台代码,后台代码全部执行完毕,不报错,但是就是不提示下载。
我的代码如下:
<input type="button" value="导出到Excel" onclick="ExportToExcel2()" />
<script type="text/javascript">
function ExportToExcel2() {
$.ajax({
type: "post",
url: "welcome.aspx?action=export2&time=" + Math.random,
success: function (data) {
alert('导出到Excel成功');
},
error: function () {
alert('导出到Excel失败!');
}
});
}
</script>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["action"] == "export2")
{
DataTable dt = Session["exportTable"] as DataTable;
ExportToExcel2(dt);
}
}
public void ExportToExcel2(DataTable dt)
{
System.Web.UI.WebControls.GridView gridview = new System.Web.UI.WebControls.GridView();
//创建DataGrid控件,并把要到处的datatable数据绑定到DataGrid控件上(导出DataGrid上的值就是导出需要的datatable)
gridview.ID = "excelGrid1";
gridview.DataSource = dt;
gridview.DataBind();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition","attachment;filename=盘存表明细统计.xls");
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.Default;
gridview.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gridview.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
我解决了,呵呵,这里分享一下,可以在按钮事件中跳转window.open("test_export.aspx");
然后在test_export.aspx.cs页面的load事件中写导出为excel代码 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询