如题:asp.net在导出提示“服务器无法在发送 HTTP 标头之后设置内容类型”。具体代码如下:
/// <summary>
/// 导出方法
/// </summary>
/// <param name="ctr"></param>
/// <param name="FileType">导出类型</param>
/// <param name="FileName">导出文件名</param>
public void GridViewToExcel(Control ctrl, string FileType, string FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
2、导出按钮
DateTime dt = System.DateTime.Now;
string str = dt.ToString("药品采购入库明细MMddhhmmss");
str = str + ".xls";
GridViewToExcel(this.view_yprkmx, "application/ms-excel", str);
ScriptManager.RegisterStartupScript(Page, typeof(Page), "提示", "alert('导出成功!');", true);
以上是代码,但是前台导出时提示:“服务器无法在发送 HTTP 标头之后设置内容类型”,如下图:
求大神指点!!!