如何将多个页面的gridview数据导出到excel文档中?

时间:2016-03-06 09:43:17

标签: asp.net excel

我在我的C#asp.net表单应用程序中使用了gridview,并启用了#34; AllowPaging"现在每当我想将它转换为excel时,只显示第一页。使用" HtmlTextWriter"完成转换。和" StringWriter"。

- 提前谢谢。

2 个答案:

答案 0 :(得分:1)

要从gridview导出excel,您可以在项目中使用以下类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;

namespace gride_edit
{
    public class GridToExcel
    {
        public static void ExportToExcel(string strFileName, GridView gv)
        {
            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)
                    {
                        PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }

                    //  add each of the data rows to the table  
                    foreach (GridViewRow row in gv.Rows)
                    {
                        PrepareControlForExport(row);
                        row.Style["background-color"] = "#FFFBD6";
                        table.Rows.Add(row);
                    }

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

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

                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", strFileName));
                    HttpContext.Current.Response.ContentType = "application/ms-excel";
                    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

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

        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 HiddenField)
                {
                    control.Controls.Remove(current);
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }

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

并可以调用此函数进行导出。

GridToExcel.ExportToExcel("Report" + DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + ".xls", GridView1);

要导出一个excel来自多个分页gridview,只需在调用convert函数&#34; ExportToExcel()&#34;之前使用这些行。

GridView1.AllowPaging = false;
bindGridView();

我得到了#34; GridToExcel&#34;从互联网上课。它对我有用,希望它也适合你。请告诉我。

答案 1 :(得分:0)

通常,您可以使用以下功能

 public void Export(HttpResponseBase Response, object data)
 {
        var gridItem = new System.Web.UI.WebControls.GridView();
        gridItem.DataSource = data;
        gridItem.DataBind();
        StringWriter writer = new StringWriter();
        HtmlTextWriter html = new HtmlTextWriter(writer);

        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=result.xls");
        Response.ContentType = "application/excel";

        gridItem.RenderControl(html);
        Response.Write(writer.ToString());
        Response.End();
 }

您可以将此功能称为

var details = /* your query result goes here */ ;
Export(Response , details );

希望它可以帮到你

相关问题