在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
复制代码 代码如下: /// /// 将DataTable中的数据导出到指定的Excel文件中 /// /// Web页面对象 /// 包含被导出数据的DataTable对象 /// Excel文件的名称 public static void Export(System.Web.UI.Page page,System.Data.DataTable tab,string FileName) { System.Web.HttpResponse httpResponse = page.Response; System.Web.UI.WebControls.DataGrid dataGrid=new System.Web.UI.WebControls.DataGrid(); dataGrid.DataSource=tab.DefaultView; dataGrid.AllowPaging = false; dataGrid.HeaderStyle.BackColor = System.Drawing.Color.Green; dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; dataGrid.HeaderStyle.Font.Bold = true; dataGrid.DataBind(); httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8)); //filename="*.xls"; httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); httpResponse.ContentType ="application/ms-excel"; System.IO.StringWriter tw = new System.IO.StringWriter() ; System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw); dataGrid.RenderControl(hw); string filePath = page.Server.MapPath("..")+"//Files//" +FileName; DownFile(httpResponse,FileName,filePath); httpResponse.End(); Response.AppendHeader("Content-Disposition","attachment;filename=" + |
请发表评论