出错1:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
解决方案:在后台文件中重载VerifyRenderingInServerForm方法,如:
public override void VerifyRenderingInServerForm(Control control)
{
}

出错2:只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render();

解决方案:在源中,添加红色部分<%@ Page Language="C#" EnableEventValidation = "false"CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

另外,在使用时,把GRIDVIEW获取数据返回DataSet类型数据,或者返回SqlDataReader类型数据也可以。

调用事件里,实例化一个DATASET, 让它值等于DS

         private void GridViewBind()
{
string sortExpression = this.grid.Attributes["SortExpression"] == null ? "Code" : this.grid.Attributes["SortExpression"];
bool isASCDirection = true;
if (grid.Attributes["SortDirection"] == "DESC")
{
isASCDirection = false;
}
int totalCounts = (int)SqlHelper.GetCountNumber("WS_User", "ID", ltConditon.Text);
AspNetPager1.RecordCount = totalCounts;
grid.DataSource = SqlHelper.GetPagedData("WS_User", ltConditon.Text, sortExpression, isASCDirection, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
grid.DataBind();
}
     private void GridBindAll()
{
string sql = "SELECT * FROM WS_User";
SqlDataReader dr = SqlHelper.ExecuteDataReader(sql, CommandType.Text);
grid.DataSource = dr;
grid.DataBind();
}
protected void btnExcel_Click(object sender, EventArgs e)
{
grid.AllowPaging = false;
grid.AllowSorting = false;
grid.Columns[].Visible = false;
GridBindAll();
DateTime dt = DateTime.Now;
string str = dt.ToString("yyyyMMddhhmmss");
str = str + ".xls";
GridViewToExcel(grid, "application/ms-excel", str); grid.AllowPaging = true;
grid.AllowSorting = true;
grid.Columns[].Visible = true;
GridViewBind();
// Export(gvRecord, "application/ms-excel", str);
}
/// <summary>
/// 将网格数据导出到Excel
/// </summary>
/// <param name="ctrl">网格名称(如GridView1)</param>
/// <param name="FileType">要导出的文件类型(Excel:application/ms-excel)</param>
/// <param name="FileName">要保存的文件名</param>
public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />" + tw.ToString());
HttpContext.Current.Response.End(); }
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}

注意:只是导出了在GridView中的列,没有把数据库中的其它没有展示的列都导出。

C# GridView 导出Excel表的更多相关文章

  1. thinkphp导入导出excel表单数据

    在PHP项目经常要导入导出Excel表单. 先去下载PHPExcel类库文件,放到相应位置. 我在thinkphp框架中的位置为ThinkPHP/Library/Org/Util/ 导入 在页面上传e ...

  2. 【ITOO 1】将List数据导出Excel表

    需求描述:在课表导入的时候,首先给用户提供模板(excel),然后将用户填写好的数据读取到list集合中.再进行判空处赋值处理,以及去重处理.这篇博客,主要介绍读取excel表和导出excel表的方法 ...

  3. ASP.NET导出excel表方法汇总

    asp.net里导出excel表方法汇总  1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...

  4. DateGridew导出Excel表+常见错误提示

    在敲机房收费系统的时候,显示数据的时候需要将DateGridew 中的数据导出进Excel表.DateGridew导出Excel表是比较常见的,当然导出Excel表有很多种方法,下面是个人认为比较容易 ...

  5. 传参导出Excel表乱码问题解决方法

    业务场景 先描述一下业务场景,要实现的功能是通过搜索框填写参数,然后点击按钮搜索数据,将搜索框的查询参数获取,附加在链接后面,调导Excel表接口,然后实现导出Excel功能.其实做导Excel表功能 ...

  6. 前端导出excel表

    前端导出excel表 方式一: 前端js实现 : https://www.cnblogs.com/zhangym118/p/6235801.html 方式二: java后端实现: https://bl ...

  7. C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation

    C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation 后台添加以下方法:/// <summa ...

  8. vue+element UI如何导出excel表

    导出excel表应按如下规则 首先要先安装如下依赖 npm install --save xlsx npm install --save file-saver 接下在在你的代码中去引用这两个 impo ...

  9. 微擎 人人商城 导出excel表分析

    在 数据处理上 ,有很多时候需要导出excel表  来当报表, 等 ,  php  人人商城导出报表过程简单分析 在导出时候发现 ca('statistics.order.export'); 出于好奇 ...

随机推荐

  1. 用Word 写csdn blog

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  2. iOS开发--使用lipo命令制作模拟器与真机通用静态库

    通常在项目中使用静态库的时候都会有两个版本,一个用于模拟器,一个用于真机,因为Mac和iPhone的CPU不同,才造成了这种情况. 为了模拟器与真机之间切换调试的方便,制作通用版本非常有必要. 现在有 ...

  3. 理解Javascript的Prototype

    在Javascript中创建对象主要分为三种方式 1. var catA = {name: "Fluffy", color: "White", age: 0}; ...

  4. numpy.convolve()

    卷积函数: numpy.convolve(a, v, mode='full') Parameters: a : (N,) array_like First one-dimensional input ...

  5. CF869C The Intriguing Obsession(组合数学瞎搞,O(n)莫名过)

    — This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...

  6. 使用ffmpeg+crtmpserver搭建文件的伪直播

    Tutorial: How to "live stream" a media file 如何"直播"一个媒体文件 I have tried a while to ...

  7. GitHub操作总结

    GitHub操作总结 : 总结看不明白就看下面的详细讲解. . 作者:万境绝尘 转载请注明出处:http://blog.csdn.net/shulianghan/article/details/188 ...

  8. 6、Semantic-UI之动画按钮样式

    6.1 动画按钮样式 在Semantic-UI中提供了三种动画样按钮式表,分别为: 左右移动 上下移动 淡入淡出   在实际开发中,很少使用这种动画按钮,根据实际情况使用,强制使用到页面中反而不太适合 ...

  9. Java API研究:获取本地环境所有网卡及每个网卡的所有网络配置

    一个网卡(不太标准,应该叫做一个网络接口,一个网卡是可以拥有多个网络接口的,如SoftAP)拥有一套网络配置:ip地址,子网掩码,网关,dns等等. 自java 1.6开始,提供了访问网络配置的一些接 ...

  10. 解读DbContext中的三种【EagerLoad,LazyLoad,ExplicitLoading】加载模式

    一:立即加载,延迟加载,显式加载 1. 立即加载 Student Domain{ Teacher theacher} using (SchoolDBEntities db = new SchoolDB ...