/// <summary>
/// 根据传入的控件并导出为Excel文件
/// </summary>
/// <param name="source">控件名</param>
/// <param name="filename">导出文件名</param>
public void ExportExcel(System.Web.UI.Control source, string filename)
{
//设置Http的头信息,编码格式Excel
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
source.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//输出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
}
在Response.End();之前,增加下面的二句试试:
Response.ContentType = "text/html";
page.RegisterStartupScript("onload", "<script language=javascript>window.location=window.location;window.alert();</script>");
迅雷的设置当中没有.xls这种格式的文件,所以下载不了。只能通过浏览器来下载了。。
一般情况下,迅雷会关联所有下载文件类型,
解决方案:
http://blog.csdn.net/ytc666/archive/2009/11/18/4830828.aspx