导出查询数据生成EXCEL文件并下载
2010-04-26 17:12
525 查看
/// <summary> /// 生成EXCEL /// </summary> public 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.GetEncoding("GB2312"); response.ContentType = "application/vnd.ms-excel"; response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>"); string file = HttpUtility.UrlEncode(FileName + ".xls", System.Text.Encoding.GetEncoding("GB2312")); response.AddHeader("Content-Disposition", "attachment; filename=" + file); response.Charset = "GB2312"; response.Write(sw.ToString()); response.End(); } private static void RearDecorator(HtmlTextWriter writer) { writer.WriteEndTag("Body"); writer.WriteEndTag("HTML"); } //入口 public void ToExcel(DataTable dt, string FileName) { System.Web.UI.WebControls.DataGrid dgTemp = new System.Web.UI.WebControls.DataGrid(); dgTemp.DataSource = dt; dgTemp.DataBind(); ToExcel(dgTemp, FileName, "", ""); } //FrontDecorator方法 private static void FrontDecorator(HtmlTextWriter writer) { writer.WriteFullBeginTag("HTML"); writer.WriteFullBeginTag("Head"); writer.WriteEndTag("Head"); writer.WriteFullBeginTag("Body"); } //// <summary> /// 导出Excel /// </summary> /// <param name="dt">表</param> public static void InitExcel(DataTable dtt) { DataGrid dgExport = null; StringWriter strWriter = null; HtmlTextWriter htmlWriter = null; System.Data.DataTable dt = dtt; if (dt != null) { HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>"); HttpContext.Current.Response.Charset = ""; strWriter = new StringWriter(); htmlWriter = new HtmlTextWriter(strWriter); dgExport = new DataGrid(); dgExport.DataSource = dt.DefaultView; dgExport.AllowPaging = false; dgExport.HeaderStyle.ForeColor = System.Drawing.Color.Blue; dgExport.HeaderStyle.BackColor = System.Drawing.Color.Red; dgExport.DataBind(); dgExport.RenderControl(htmlWriter); HttpContext.Current.Response.Write(strWriter.ToString()); HttpContext.Current.Response.End(); } } protected void Button1_Click(object sender, EventArgs e) { //CMS.BLL.t_user_infoM userinfo = new CMS.BLL.t_user_infoM(); //IDataReader ida = userinfo.GetExeCmdResult("[dbo].[sp_t_elite_week_return_check3] @game_id=1,@sta_id=1,@order_expression='wrt.sta_taxis'"); CMS.BLL.NewsManage nm = new CMS.BLL.NewsManage(); IDataReader ida = nm.GetExeCmdResult("select * from cms_newst"); DataTable dt = CMS.CommonUtility.BXDBUtility.DataReaderToDataTable(ida); this.ToExcel(dt,"aaa"); }
相关文章推荐
- 导出(下载)数据库查询的数据生成csv文件
- java简单的从数据库查询数据使用POI导出excel文件
- 把数据库查询信息直接生成网页嵌入EXCEL表的格式,并可导出成EXCEL文件
- MYSQL5.1 WINDOWS环境下导出查询数据到EXCEL文件
- 【POI】导出excel文件,不生成中间文件,直接将内存中的数据创建对象下载到浏览器
- 导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件
- input file 文件上传下载 查询数据库数据并导出Excel
- asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excel-xls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;
- 完成用户数据的导出,生成文本文件Users.txt,实现点击按钮自动下载
- 数据表格,查询、导出共用一个form表单,实现文件流方式下载
- 导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件
- Java ssh poi 根据条件查询数据导出excel到浏览器 下载框不显示 浏览器乱码 之前是用submit()
- 使用SSIS生成数据导出为Excel文件的模板
- 借鉴+总结!! mysql 客户端命令行下 查询数据并生成文件导出
- ASP.NET DataSet数据生成Excel文档导出下载
- java查询数据导出excel并返回给浏览器下载
- 数据导出文件生成并下载 java
- 使用POI 将数据库中的数据生成Excel文件并下载
- JAVA项目后台查询的数据生成Excel表格并提供给用户下载
- 【原】.Net创建Excel文件(插入数据、修改格式、生成图表)的方法 .