出口的GridView行Excel工作表 [英] export gridview rows to excel sheet

查看:159
本文介绍了出口的GridView行Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目导出网格视图行Excel工作表我serached有任务,我不得不code,我做到了,但这一类从网格视图出口仅当前页面,所以我想也有以出口的所有GridView的行。

类:

 使用系统;
使用System.Data这;
使用System.Configuration;
使用System.IO;
使用的System.Web;
使用System.Web.Security;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;///<总结>
///
///< /总结>
公共类GridViewExportUtil
{
    ///<总结>
    ///
    ///< /总结>
    ///< PARAM NAME =文件名>< /参数>
    ///< PARAM NAME =GV>< /参数>
    公共静态无效的出口(字符串文件名,GridView的GV)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            内容处置的String.Format(附件;文件名= {0},文件名));
        HttpContext.Current.Response.ContentType =应用程序/ MS-Excel的;    使用(StringWriter的SW =新的StringWriter())
    {
        使用(HTW的HtmlTextWriter =新的HtmlTextWriter(SW))
        {
            //创建一个表单包含网格
            表的表=新表();            //标题行添加到表
            如果(gv.HeaderRow!= NULL)
            {
                GridViewExportUtil prepareControlForExport(gv.HeaderRow)。
                table.Rows.Add(gv.HeaderRow);
            }            //每个数据行添加到表
            的foreach(在gv.Rows GridViewRow行)
            {
                GridViewExportUtil prepareControlForExport(行)。
                table.Rows.Add(行);
            }            //尾行添加到表
            如果(gv.FooterRow!= NULL)
            {
                GridViewExportUtil prepareControlForExport(gv.FooterRow)。
                table.Rows.Add(gv.FooterRow);
            }            //渲染表到的HTMLWriter
            table.RenderControl(HTW);            //渲染的HTMLWriter到响应
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }
}///<总结>
///更换任何包含的控件与文本
///< /总结>
///< PARAM NAME =控制>< /参数>
私有静态无效prepareControlForExport(控制控制)
{
    的for(int i = 0; I< control.Controls.Count;我++)
    {
        控制电流= control.Controls [I];
        如果(电流的LinkBut​​ton)
        {
            control.Controls.Remove(电流);
            control.Controls.AddAt(I,新LiteralControl((电流LinkBut​​ton的)。文本));
        }
        否则,如果(电流的ImageButton)
        {
            control.Controls.Remove(电流);
            control.Controls.AddAt(I,新LiteralControl((电流的ImageButton).AlternateText));
        }
        否则,如果(电流为超链接)
        {
            control.Controls.Remove(电流);
            control.Controls.AddAt(I,新LiteralControl((当前为超链接)。文本));
        }
        否则,如果(电流的DropDownList)
        {
            control.Controls.Remove(电流);
            control.Controls.AddAt(I,新LiteralControl((电流的DropDownList).SelectedItem.Text));
        }
        否则,如果(目前是复选框)
        {
            control.Controls.Remove(电流);
            control.Controls.AddAt(I,新LiteralControl((电流复选框).Checked真:假));
        }        如果(current.HasControls())
        {
            GridViewExportUtil prepareControlForExport(电流)。
        }
    }
}
}

CS:

 保护无效imgbtn_export_Click(对象发件人,ImageClickEventArgs E)
    {
        GridViewExportUtil.Export(Problems.xls,this.GridView1);
    }


解决方案

请问你的GridView适用什么特别的,是不是已经在它绑定到数据源的数据?如果没有,我会建议你直接从GridView控件绑定到同一数据导出,从而避免不必处理分页。

I had task in my project to export grid view rows to excel sheet I serached and I had code and I did it but this class export only current page from grid view so I want also to export all gridview rows.

Class:

using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 
/// </summary>
public class GridViewExportUtil
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="gv"></param>
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            //  Create a form to contain the grid
            Table table = new Table();

            //  add the header row to the table
            if (gv.HeaderRow != null)
            {
                GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                table.Rows.Add(gv.HeaderRow);
            }

            //  add each of the data rows to the table
            foreach (GridViewRow row in gv.Rows)
            {
                GridViewExportUtil.PrepareControlForExport(row);
                table.Rows.Add(row);
            }

            //  add the footer row to the table
            if (gv.FooterRow != null)
            {
                GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                table.Rows.Add(gv.FooterRow);
            }

            //  render the table into the htmlwriter
            table.RenderControl(htw);

            //  render the htmlwriter into the response
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }
}

/// <summary>
/// Replace any of the contained controls with literals
/// </summary>
/// <param name="control"></param>
private static void PrepareControlForExport(Control control)
{
    for (int i = 0; i < control.Controls.Count; i++)
    {
        Control current = control.Controls[i];
        if (current is LinkButton)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
        }
        else if (current is ImageButton)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
        }
        else if (current is HyperLink)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
        }
        else if (current is DropDownList)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
        }
        else if (current is CheckBox)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
        }

        if (current.HasControls())
        {
            GridViewExportUtil.PrepareControlForExport(current);
        }
    }
}
}

CS:

 protected void imgbtn_export_Click(object sender, ImageClickEventArgs e)
    {
        GridViewExportUtil.Export("Problems.xls", this.GridView1);
    }

解决方案

Does your gridview apply anything special to the data that is not already in the data source that it is bound to? If not, I would suggest you export directly from the same dataset that the gridview is bound to, thereby avoiding having to deal with pagination.

这篇关于出口的GridView行Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆