在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
如下是导出到TXT的方法 /// <summary> /// Renders the html text before the datagrid. /// </summary> /// <param name="writer">A HtmlTextWriter to write html to output stream</param> private static void FrontDecorator(HtmlTextWriter writer) { writer.WriteFullBeginTag("HTML"); writer.WriteFullBeginTag("Head"); writer.WriteEndTag("Head"); writer.WriteFullBeginTag("Body"); } /// <summary> /// Renders the html text after the datagrid. /// </summary> /// <param name="writer">A HtmlTextWriter to write html to output stream</param> private static void RearDecorator(HtmlTextWriter writer) { writer.WriteEndTag("Body"); writer.WriteEndTag("HTML"); } public static void ToExcel(System.Web.UI.WebControls.DataGrid DataGrid2Excel,string FileName,string Title, string Head) { System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); FrontDecorator(hw); if ( Title != "") hw.Write(Title + "<br>"); if ( Head != "") hw.Write(Head + "<br>"); DataGrid2Excel.EnableViewState = false; DataGrid2Excel.RenderControl(hw); RearDecorator(hw); System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear(); response.Buffer = true; response.ContentEncoding = System.Text.Encoding.Default; response.ContentType ="application/Excel"; response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls"); response.Charset = "gb2312"; response.Write(sw.ToString()); response.End(); }
|
请发表评论