HttpContext.Current.Response.Clear(); 
HttpContext.Current.Response.Buffer= true; 
HttpContext.Current.Response.Charset="GB2312";    
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=RFSMainInquiryResult.xls"); 
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//扢离怀堤霜峈潠极笢恅
HttpContext.Current.Response.ContentType = "application/ms-excel";//扢离怀堤恅璃濬倰峈excel恅璃﹝ 
DatagridApproval.EnableViewState = false;    
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
DatagridApproval.RenderControl(oHtmlTextWriter); 
HttpContext.Current.Response.Write(oStringWriter.ToString());
HttpContext.Current.Response.End(); -----------------------
上面是我的代碼,問題是我的datagrid是分几頁顯示的,用上面的代碼只導出當前看的那一頁

解决方案 »

  1.   

    你用上面的代码导出,必须是干净的datagrid,不能有其他标签,分页了什么都不能有.要导出就得写复杂的代码一行一列地读取导出了,网上的代码也很多.我以前的作法是再作一个无分页的datagrid在页面上隐藏起来,和上面的datagrid绑定同样的数据,再用上面的那种简单的代码导出来.实现了就可以了.