You must Sign In to post a response.
  • Category: ASP.NET

    More than one Grid Data Export into One Excel sheet

    I have two Grid View , Grid View is Bind at the Page Load.
    After Page Load Grid View Data Will show.

    I want both Grid Data should be Export into Excel, on a single button click.
    How I can do it.
  • #646213
    Please check this below article for Exporting Multiple Grid to the same Excel sheet. You can use Multiple sheet to export the grid.


    aspsnippets.com/Articles/Exporting-Multiple-GridViews-To-Excel-SpreadSheet-in-ASP.Net.aspx

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #646312
    Dear friend,
    Just merge both grid view data tables into single data table.

    Then try like this


    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using System.Data;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    BindData();
    }

    }
    private void BindData()
    {
    string query = "select * from a1";
    SqlConnection myConnection = new SqlConnection("server=servername;uid=userid;password=password;database=databasename");
    SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
    DataSet ds = new DataSet();
    ad.Fill(ds, "TableName");
    grvGridToExcel.DataSource = ds;
    grvGridToExcel.DataBind();
    }


    public override void VerifyRenderingInServerForm(Control control)
    {
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
    server control at run time. */

    }
    protected void BtnExport_Click(object sender, EventArgs e)
    {
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    grvGridToExcel.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
    }

    }


    In Aspx File


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="grvGridToExcel" runat="server">
    </asp:GridView>
    <asp:Button ID="BtnExport" OnClick="BtnExport_Click" runat="server" Text="ExportGridtoExcel" />
    </div>
    </form>
    </body>
    </html>



    Thanks & Regards
    G.Renganathan
    Nothing is mine ,Everything is yours!!!


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.