GridView中的数据导出到Excel
2012-04-19 10:15
465 查看
将页面中的数据绑定控件(如GridView、ListView等)中的数据导出到Excel中是个很常用的功能,Google相关资料后总结如下:
一、自定义一个方法:ToExcel(Control ctl, string FileName)
这个方法就是将数据绑定控件中的数据导出到Excel。
二、双击LinkButton,在按钮中写入如下代码:
三、当然了,还有自定义的绑定方法bang()。写这个bang()就不用在绑定数据时写一大堆的代码了。bang()代码略。
其实将数据导出为Excel应该是很常用的一个功能,为避免在不同的页面中都写一遍代码,我们可以将其做成一个类模块,保存在App_Code文件夹中,这样需要用到的时候就可以直接引用了。
然后在CS文件中引用类模块:
注意!很容易出现这个错误提示:
这样就不会有错误了。
运用类模块后,在任何需要导出到excel的页面中只需要简单的引用就可以了,主要就是:
另一篇导出到Excel的文章:/article/5323672.html
一、自定义一个方法:ToExcel(Control ctl, string FileName)
这个方法就是将数据绑定控件中的数据导出到Excel。
private void ToExcel(Control ctl, string FileName) { HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName); ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); }
二、双击LinkButton,在按钮中写入如下代码:
protected void LinkButton1_Click(object sender, EventArgs e) { GridView1.AllowPaging = false; GridView1.AllowSorting = false; bang(); ToExcel(GridView1, "data.xls"); GridView1.AllowPaging = true; GridView1.AllowSorting = true; bang(); }
三、当然了,还有自定义的绑定方法bang()。写这个bang()就不用在绑定数据时写一大堆的代码了。bang()代码略。
其实将数据导出为Excel应该是很常用的一个功能,为避免在不同的页面中都写一遍代码,我们可以将其做成一个类模块,保存在App_Code文件夹中,这样需要用到的时候就可以直接引用了。
App_Code文件: using System; using System.Collections.Generic; using System.Linq; using System.Web; // using System.Web.UI; public class toExcel { public toExcel() { // //TODO: 在此处添加构造函数逻辑 // } public Control ctl; public string FileName; public void exportExcel(Control ctl, string FileName) { HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName); ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); } }
然后在CS文件中引用类模块:
protected void LinkButton1_Click(object sender, EventArgs e) { GridView1.AllowPaging = false;//取消分页 GridView1.AllowSorting = false;//取消排序 bind(); toExcel Export=new toExcel(); Control ctl = Export.ctl = GridView1; string FileName = Export.FileName = "我的表格.xls"; Export.exportExcel(ctl, FileName); GridView1.AllowPaging = true;//恢复分页 GridView1.AllowSorting = true;//恢复排序 bind(); }
注意!很容易出现这个错误提示:
类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内
如果是这样,需要重载一个方法(方法中什么都不写即可。):public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); }
这样就不会有错误了。
运用类模块后,在任何需要导出到excel的页面中只需要简单的引用就可以了,主要就是:
toExcel Export=new toExcel(); Control ctl = Export.ctl = GridView1; string FileName = Export.FileName = "我的表格.xls"; Export.exportExcel(ctl, FileName);
另一篇导出到Excel的文章:/article/5323672.html
相关文章推荐
- 将GridView中的数据导出到Excel中下载并且解决乱码的问题
- 在ASP.NET中将GridView数据导出到Word、Excel
- 将gridview中的数据导出到excel中
- asp.net 导出 gridview 数据 excel 全部 当前页 选择行 选中行 所选行 复选框
- ASP.NET GridView导出数据到Excel
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- 将dev gridview中数据原样导出到EXCEL
- GridView中的数据导出为Excel【转】,和以前的有变化
- Gridview中的数据导出到excel中
- 导出 Gridview数据到 Excel
- 导出GridView中的数据到Excel
- 不需要安装office excel,导出GridView所有数据为Excel
- GridView数据导出到Excel
- 从gridview 中查询数据导出Excel 简单方法
- 【asp.net】将GridView数据导出Excel
- 从Gridview 中导出数据到EXCEL
- gridview数据如何导出到word,excel
- .net从gridview控件导出数据到excel续
- asp.net<Web版> ---GridView 数据导出到excel表,系列问题<一>---导出的Excel文件格式与文件扩展名指定的格式不一致