public static bool PrintExcel(DataGrid DG)
{
if(DG.AllowPaging)
{
return false;
}
try
{
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.Web.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename="
+   HttpUtility.UrlEncode("DownLoad.xls",Encoding.Default )   );    //如果输出为Word,修改为以下代码
//Response.ContentType = "application/ms-word" 
//Response.AddHeader("Content-Disposition", "inline;filename=test.doc") 
StringBuilder sb=new StringBuilder(); 
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
//System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
    
sb.Append("<html><body>");
DG.RenderControl(hw);
sb.Append("</body></html>"); 
System.Web.HttpContext.Current.Response.Write(sb.ToString());
System.Web.HttpContext.Current.Response.End();
return true;
}
catch(Exception)
{
System.Web.HttpContext.Current.Response.End();
return false;
}

}