GridView中导出表格的代码
来源:互联网 发布:网络歌曲大全2017 编辑:程序博客网 时间:2024/06/05 07:55
在GridView中还是经常会遇到导出Excel,现在把导出gridView数据代码记录下来,仅供参考 如果有更好的方法,也可以交流
一、先建一个类,其中的一个方法为ToExcel。代码如下
public static void ToExcel(Control ctl, string FileName) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = System.Text.Encoding.Default.HeaderName; //HttpContext.Current.Response.Charset = "Unicode"; //HttpContext.Current.Response.Charset = "gb2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"); ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">"); //HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"></head>"); HttpContext.Current.Response.Write(tw.ToString()); //HttpContext.Current.Response.Write("</body></html>"); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); }
二、在含有Gridview中的页面中后台代码引用
2.1引用前要在前台代码头文件中把 EnableEventValidation="false" 设置一下
2.2在后台开始引用
一个是button按钮事件(ps:其中GvFloorList为GridView的ID)
protected void btnExcel_Click(object sender, EventArgs e) { if (this.GvFloorList.Rows.Count > 0) { int page = this.GvFloorList.PageIndex; this.GvFloorList.AllowPaging = false; this.GvFloorList.AllowSorting = false; BindData(); Common.ToExcel(GvFloorList, "表格名"); this.GvFloorList.AllowPaging = true; this.GvFloorList.AllowSorting = true; BindData(); this.GvFloorList.PageIndex = page; } }
还有一个是要在后台代码中添加一个继承方法
#region VerifyRenderingInServerForm /// <summary> /// VerifyRenderingInServerForm /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } #endregion
- GridView中导出表格的代码
- 从GridView控件以Excel表格导出代码的注释
- 最简单实用的GridView导出到Excel表格代码
- Repeter和GridView的数据导出到EXCEL表格中
- 导出EXCEL表格的代码
- 在AJAX中从GridView表中无法导出Excel表格的问题
- 从GridView中导出Excel代码
- gridview中数据的导出
- 把GridView的列表数据导出为Execl表格文件
- 把GridView的列表数据导出为Word表格文件
- GridView和Reapter导出Excel表格的总结
- GridView导出成EXCEL的代码
- GridView导出Excel代码
- ASP.NET 中直接把gridview的视图内容导出到excel的关键代码
- 从GRIDVIEW中导出EXCEL的方法
- 关于Asp.net中使用以下代码导出Excel表格的问题
- 关于Asp.net中使用以下代码导出Excel表格的问题
- 下面代码实现将 GridView 导出到 Excel文件中。
- C++ static 变量使用
- sql server 2005中with ties的使用
- 时间相减
- 大秦帝国(经典台词)
- 用js绘制矩形框
- GridView中导出表格的代码
- IBATIS配置文件sqlmap-config.xml之乱配
- 一些CSS错误及解决办法
- 字符串哈希算法比较(转载)
- 《设计模式之禅》学习之策略模式
- Shell脚本编程剖析
- QT 无法解析的外部符号 QUiLoader::~QUiLoader l QUiLoader::QUiLoader 原来是没有QtUiTools.lib
- Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。
- DWR