ASP.NET中GridView数据导出到Excel
2016-06-16 09:18
661 查看
/// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnExport_Click1(object sender, EventArgs e) { this.gv.AllowPaging = false; this.gv.AllowSorting = false; Query();//获取数据并绑定到GridView toExcel(this.gv); this.gv.AllowPaging = true; this.gv.AllowSorting = true; Query();//获取数据并绑定到GridView } /// <summary> /// 导出到Excel /// </summary> /// <param name="gv"></param> void toExcel(GridView gv){ Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); string fileName = "export.xls"; string style = @"<style> .text { mso-number-format:\@; } </script> "; Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=" + fileName); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); this.gv.RenderControl(htw); Response.Write(style); Response.Write(sw.ToString()); Response.End(); } /// <summary> /// 这个重写貌似是必须的 /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { } 备注: 1、gv就是页面上的GridView空间名称; 2、代码中的Query方法所做的是,获取数据并绑定到GridView。
相关文章推荐
- ASP.NET Core 中文文档 第二章 指南(4.4)添加 Model
- asp.net core 中间件详解及项目实战
- SpringMVC与iReport(JasperReports) 整合开发实例教程
- ASP.NET 上传文件最大值调整
- Java 8: 从永久代(PermGen)到元空间(Metaspace)
- 建筑材料系统 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发
- ASP.NET Web API 入门示例详解
- ASP.NET应用程序与页面生命周期
- ASP.NET样板项目ABP框架的特性总结
- Asp.net GridView 应用实例
- 基于ASP.NET MVC的ABP框架入门学习教程
- ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询
- ASP.NET相关笔记总结
- 使用Gridview控件的链接实现页面跳转并且传值
- IIS 7.0, ASP.NET, pipelines, modules, handlers, and preconditions
- ASP.NET Core1.0 带来的新特性
- ASP.NET 允许跨域请求
- 【翻译整理】ASP.NET Web API 2
- ASP.NET MVC基于标注特性的Model验证:ValidationAttribute
- aspnet调用mysql存储过程