public class ExportHelper { //导出execl private void Export(string fileType, string fileName, GridView grd) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8)); HttpContext.Current.Response.ContentType = fileType; grd.EnableViewState = false; var tw = new StringWriter(); var hw = new HtmlTextWriter(tw); DisableControls(grd); grd.RenderControl(hw); const string strStyle = "<style>.xlString { mso-number-format:\\@; } </style>"; HttpContext.Current.Response.Write(strStyle); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.Write(strStyle); HttpContext.Current.Response.End(); } //将button,linkbutton,dropdownlist 都转换成为文本 private void DisableControls(Control gv) { var l = new Literal(); for (int i = 0; i < gv.Controls.Count; i++) { Type type = gv.Controls[i].GetType(); if (type == typeof(LinkButton)) { l.Text = ((LinkButton)gv.Controls[i]).Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (type == typeof(DropDownList)) { l.Text = ((DropDownList)gv.Controls[i]).SelectedItem.Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (type == typeof(Button)) { l.Text = ((Button)gv.Controls[i]).Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } if (gv.Controls[i].HasControls()) { DisableControls(gv.Controls[i]); } } } }