asp.net_GridView导出数据到Excel/Word

本文介绍了如何将ASP.NET GridView中的数据显示导出到Excel和Word文件,包括实际导出效果的展示,并提及了代码中if语句用于筛选数据。

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

      之前做过一个这样的项目,需要把Girdiew查找出来的数据导出为Excel文件和Word文件,现在先收集起来。当初录入的是同学的名字,来个高斯模糊先~


    //导出excel
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;

        string strsql = @"SELECT class.class_name, users.user_name, COUNT(late.user_id) AS cishu FROM class INNER JOIN
        users ON class.class_id = users.class_id INNER JOIN
        late ON users.user_id = late.user_id";

        if (dept.SelectedValue != "")
        {
            strsql += " and users.dept_id=" + dept.SelectedValue;
        }
        if (special.SelectedValue != "")
        {
            strsql += " and users.spc_id=" + special.SelectedValue;
        }
        if (room.SelectedValue != "")
        {
            strsql += " and users.class_id=" + room.SelectedValue;
        }
        if (region.SelectedValue != "")
        {
            strsql += " and late.region_id=" + region.SelectedValue;
        }
        if (txtKs.Text != "")
        {
            strsql += " and late.late_time>='" + txtKs.Text + "'";
        }
        if (txtJs.Text != "")
        {
            strsql += " and late.late_time<='" + txtJs.Text + "'";
        }
        if (txtRoom.Text != "")
        {
            strsql += " and room='" + txtRoom.Text + "'";
        }
        if (txtName.Text != "")
        {
            strsql += " and user_name like '%" + txtName.Text + "%'";
        }

        strsql += " GROUP BY class.class_name, users.user_name";
        strsql += " ORDER BY cishu DESC";

        DataTable dt = db.reDt(strsql);
        GridView1.DataSource = dt;
        GridView1.DataBind();

        //一、定义文档类型、字符编码

        Response.Clear();

        Response.Buffer = true;

        Response.Charset = "gb2312";

        //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开 

        //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm 
        DateTime dtime = DateTime.Now;//给导出后的excel表命名,结合表的用途以及系统时间来命名
        string filename = dtime.Year.ToString() + dtime.Month.ToString() + dtime.Day.ToString();

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + "latecount.xls");

        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

        //Response.ContentType指定文件类型 可以为application/ms-excel    application/ms-word    application/ms-txt    application/ms-html    或其他浏览器可直接支持文档  

        Response.ContentType = "application/ms-excel";

        GridView1.EnableViewState = false;

        //二、定义一个输入流

        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        //三、将目标数据绑定到输入流输出
        GridView1.RenderControl(oHtmlTextWriter);
        //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件   

        Response.Write(oStringWriter.ToString());

        Response.End();

        GridView1.AllowPaging = true;
    }
    
    //导出word
    protected void btnWord_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;

        string strsql = @"SELECT class.class_name, users.user_name, COUNT(late.user_id) AS cishu FROM class INNER JOIN
        users ON class.class_id = users.class_id INNER JOIN
        late ON users.user_id = late.user_id";
        if (dept.SelectedValue != "")
        {
            strsql += " and users.dept_id=" + dept.SelectedValue;
        }
        if (special.SelectedValue != "")
        {
            strsql += " and users.spc_id=" + special.SelectedValue;
        }
        if (room.SelectedValue != "")
        {
            strsql += " and users.class_id=" + room.SelectedValue;
        }
        if (region.SelectedValue != "")
        {
            strsql += " and late.region_id=" + region.SelectedValue;
        }
        if (txtKs.Text != "")
        {
            strsql += " and late.late_time>='" + txtKs.Text + "'";
        }
        if (txtJs.Text != "")
        {
            strsql += " and late.late_time<='" + txtJs.Text + "'";
        }
        if (txtRoom.Text != "")
        {
            strsql += " and room='" + txtRoom.Text + "'";
        }
        if (txtName.Text != "")
        {
            strsql += " and user_name like '%" + txtName.Text + "%'";
        }

        strsql += " GROUP BY class.class_name, users.user_name";
        strsql += " ORDER BY cishu DESC";

        DataTable dt = db.reDt(strsql);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "gb2312";
        DateTime dtime = DateTime.Now;//给导出后的word表命名,结合表的用途以及系统时间来命名
        string filename = dtime.Year.ToString() + dtime.Month.ToString() + dtime.Day.ToString();
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + "latecount.doc");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.ContentType = "application/ms-word";
        GridView1.EnableViewState = false;

        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();

        GridView1.AllowPaging = true;
    }

    //重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内”
    public override void VerifyRenderingInServerForm(Control control)
    {
        //解决:控件必须放在具有 runat=server 的窗体标记内"的错误     

        //在页面中重写Page基类的VerifyRenderingInServerForm方法

        // Confirms that an HtmlForm control is rendered for
    }

导出的效果如下:

word:
excel:


代码中那种if语句,只是筛选数据的条件。闭嘴 (~﹃~)~zZ睡觉=-= 晚安

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值