protected void btnExport_Click( object sender, EventArgs e) |
{ |
gridView.DataSource =dt; //DataTable数据 |
gridView.DataBind(); |
GridView gd = new GridView(); |
gd = gridView; |
if (gd.Rows.Count != 0) |
{ |
gridView.AllowPaging = false ; |
System.Web.HttpContext con = System.Web.HttpContext.Current; |
con.Response.AddHeader( "content-disposition" , "attachment; filename=YHTZM_" + DateTime.Now.ToString( "yyyy-MM-dd" ) + ".xls" ); //设置文件名 |
System.IO.StringWriter strWriter = null ; |
System.Web.UI.HtmlTextWriter htmlWriter = null ; |
con.Response.ContentType = "application/excel" ; |
con.Response.ContentEncoding = System.Text.Encoding.UTF8; |
strWriter = new System.IO.StringWriter(); |
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); |
gridView.RenderControl(htmlWriter); |
string strExcel = strWriter.ToString(); |
//去除超链接 |
strExcel = System.Text.RegularExpressions.Regex.Replace(strExcel, "<a (.*?)>" , "" , System.Text.RegularExpressions.RegexOptions.Compiled); |
strExcel = System.Text.RegularExpressions.Regex.Replace(strExcel, "</a>" , "" ); |
con.Response.Write(strExcel.Replace( "0001-01-01" , "" )); |
gridView.AllowSorting = true ; |
con.Response.End(); |
|
} |
} |
初级程序员
by: 云代码会员 发表于:2019-04-10 10:13:25 顶(0) | 踩(0) 回复
不错 支持一下
回复评论