62,243
社区成员




<body onload="load()" style=" background:#F4F4F4;">
<form id="form1" runat="server">
<asp:Button ID="btnExport" Text="导出Excel" OnClick="btnExport_Click" OnClientClick="loadingDelay(10000)" runat="server"/>
<div id="divMain_B">
<asp:GridView ID="gdvMain" runat="server" AutoGenerateColumns="False" Width="1150px"
GridLines="None" ShowFooter="True" DataSourceID="SqlDataSource1" ShowHeaderWhenEmpty="True"
EmptyDataText=" 没有您查找的数据">
//将gridviewd的数据导出到excel文件并发送到客户端下载(将在服务器磁盘上生成一份excel文件)
protected void btnExport_Click(object sender, EventArgs e)
{
//类似下面的这行代码去改变页面控件的值,均无效果,线程sleep也不行
this.sltLoadingTag.SelectedIndex = 1;
////grid导出excel文件
ExcelHelper helper = new ExcelHelper();
helper.ExceuteGridView(this.gdvMain, Page.Response);
}
r.Clear();
r.Charset = "GB2312";
r.ContentEncoding = System.Text.Encoding.UTF8;
r.ContentType = "application/octet-stream";
//通知浏览器下载文件
r.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
r.AddHeader("Content-Length", bytes.Length.ToString());
//使用二进制流传输(若使用r.WriteFile传输,将使下载后的excel文件格式异常)
r.BinaryWrite(bytes);
r.Flush();
r.End();
ScriptManager.RegisterStartupScript(this, this.GetType(), "download", "window.open(.....); lodingCompleted();", true);
当前页面关闭loading,同时弹出窗口进行下载。
大多数浏览器,遇到这种内容类型为application/octet-stream的html页面(或者其他类似的页面),其实都不会弹出窗口,而会直接下载文件。