GridView导出Excel

/// <summary>
/// 将网格数据导出到Excel
/// </summary>
/// <param name="ctrl">网格名称(如GridView1)</param>
/// <param name="FileType">要导出的文件类型(Excel:application/ms-excel)</param>
/// <param name="FileName">要保存的文件名</param>
public void GridViewToExcel(Control ctrl, string FileType, string FileName)
{
    bool gridViewAllowPaging = false;
    if (ctrl is GridView)
    {
        gridViewAllowPaging = ((GridView)ctrl).AllowPaging;
        if (gridViewAllowPaging)
        {
            ((GridView)ctrl).AllowPaging = false;
        }
    }
    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;
    System.IO.StringWriter tw = new System.IO.StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    ctrl.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
    if (ctrl is GridView && gridViewAllowPaging)
    {
        ((GridView)ctrl).AllowPaging = true;
    }
}

别急,使用后会报错:

“/”应用程序中的服务器错误。


类型“GridView”的控件“gridView1”必须放在具有 runat=server 的窗体标记内。

说明:执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Web.HttpException: 类型“GridView”的控件“gridView1”必须放在具有 runat=server 的窗体标记内。

源错误:

行 194:            System.IO.StringWriter tw = new System.IO.StringWriter();
行 195:            HtmlTextWriter hw = new HtmlTextWriter(tw);
行 196:            ctrl.RenderControl(hw);
行 197:            HttpContext.Current.Response.Write(tw.ToString());
行 198:            HttpContext.Current.Response.End();

源文件: E:\heren\code\Z2\CPMonitor\WebCpMonitorAndStat\CPImportStatistics.aspx.cs    行:196

检查代码,gridView1确实有runat=server标记。此时加入如下代码:

public override void VerifyRenderingInServerForm(Control control) { }

如此即可。

不过有一点要注意:如果有用分页控件,要在GridViewToExcel前重新绑定所有数据到GridView控件。

时间: 2024-07-31 04:25:57

GridView导出Excel的相关文章

扩展GridView导出Excel功能

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.CompilerServices;using System.Text;using System.Web;using System.Web.Compilation;using System.Web.UI;using System.Web.UI.WebControls; namespace Web{ 

GridView 导出Excel

protected void btndaochu_Click(object sender, EventArgs e)        {            GetList();//读取GridView 数据  这里就不写了            Response.Clear();            Response.Charset = "utf-8";            Response.ContentEncoding = System.Text.Encoding.GetEn

IE下GridView导出Excel无反应

使用GridView导出Excel的方法把GridView中的内容导出为Excel: /// <summary> /// 将网格数据导出到Excel /// </summary> /// <param name="ctrl">网格名称(如GridView1)</param> /// <param name="FileType">要导出的文件类型(Excel:application/ms-excel)<

C#实现GridView导出Excel

using System.Data;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using System.Text; namespace DotNet.Utilities{    /// <summary>    /// Summary description for GridViewExport    /// </summary>    public cl

GridView导出Excel的超好样例

事实上网上有非常多关于Excel的样例,可是不是非常好,他们的代码没有非常全,读的起来还非常晦涩.经过这几天的摸索,最终能够完毕我想要导出报表Excel的效果了.以下是我的效果图. 一.前台的页面图 GridView的第一页的内容 GridView第二页的内容: 大家可能遇到这样的情况,就是这个时候导出Excel的时候,打开Excel的时候发现GridView的第二页的内容却没有导出到Excel表里面.事实上解决这样的情况,非常easy,仅仅要在导出之前,把Gridview的设置分页设置为Fla

ASP.NET GridView导出Excel

导出GridView时候没有用控件,直接用流.在多个地方需要导出Excel,出现一个问题,有的地方导出的中文正常,但是有的地方导出的中文会出现乱码的情况.查找网上相关资料,可能问题发现是编码问题.我原来输出流字符集用的是Encoding.Default,将输出流字符集改成UTF-8仍然会有问题.查找资料发现在导出的时候加上如下代码可解决问题: Response.Write(“<meta http-equiv=Content-Type content=text/html;charset=GB231

C# GridView导出excel和word文件

//导出excel按钮事件 protected void Button1_Click(object sender, EventArgs e) { Export("application/ms-excel", "农田环境数据.xls"); } //导出word按钮事件 protected void Button2_Click(object sender, EventArgs e) { Export("application/ms-excel", &

C# GridView 导出Excel表

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

dev中的gridview导出excel文件

源代码: private void ExportExcel(string fileName, DevExpress.XtraGrid.Views.Grid.GridView myDGV) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xlsx"; saveDialog.Filter = "Excel