GridView中的数据导出到Excel

本文介绍了一种将ASP.NET应用中的数据绑定控件(如GridView)数据导出到Excel的方法。通过自定义方法ToExcel及使用类模块简化操作流程,确保不同页面间的数据导出功能一致且高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将页面中的数据绑定控件(如GridView、ListView等)中的数据导出到Excel中是个很常用的功能,Google相关资料后总结如下:

一、自定义一个方法: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的文章:http://www.cnblogs.com/ibgo/p/3531799.html

转载于:https://www.cnblogs.com/ibgo/archive/2012/04/19/2456637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值