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;
}
}
{
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;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货