日期:2014-05-17  浏览次数:20460 次

response.write导出失败
操作系统 W2008 数据库server 2008
前台页面 aspx
代码 aspx.cs 中
将GRIDVIEW中的数据导出到EXECL
  protected void Btn_ExportExcelPaging()

  {

  Response.ClearContent();

  Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");

  Response.ContentType = "application/excel";

  StringWriter sw = new StringWriter();

  HtmlTextWriter htw = new HtmlTextWriter(sw);

  GvReser.RenderControl(htw);

  Response.Write(sw.ToString());

  Response.End();

  }

如果不把最后两句注释掉,则整段代码不执行
  Response.Write(sw.ToString());

  Response.End();

请各位大侠解答,帮顶有分




------解决方案--------------------
C# code

HttpContext.Current.Response.AppendHeader( "Content-Disposition ", "attachment;filename=Excel.xls ");  
     HttpContext.Current.Response.Charset   = "UTF-8 ";          
     HttpContext.Current.Response.ContentEncoding   =System.Text.Encoding.Default;  
     HttpContext.Current.Response.ContentType   = "application/ms-excel ";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword  
     ctl.Page.EnableViewState   =false;        
     System.IO.StringWriter     tw   =   new   System.IO.StringWriter()   ;  
     System.Web.UI.HtmlTextWriter   hw   =   new   System.Web.UI.HtmlTextWriter   (tw);  
     ctl.RenderControl(hw);  
     HttpContext.Current.Response.Write(tw.ToString());  
     HttpContext.Current.Response.End();

------解决方案--------------------
这个跟自己电脑上的excel组件有关系。你可以尝试直接将数据到处到excel

------解决方案--------------------
导出失败?有报什么错没???
加这个试试?
public override void VerifyRenderingInServerForm(Control control)
{

}
------解决方案--------------------
有报错么???
加这个试试??
public override void VerifyRenderingInServerForm(Control control)
{

}
------解决方案--------------------
response.write会冲掉以前的东西。不弹出是正常的
------解决方案--------------------
string name = "Rpt" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
Response.Clear();
Response.Buffer = false;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + name);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/ms-excel";
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-\">");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();


这样呢???
------解决方案--------------------
导出按钮是不是在UpdatePanel中?