Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have 2 gridviews on my page which I need to export the same in excel as
Gridview1 to Excel (Sheet 1)
Gridview1 to Excel (Sheet 2)

Please help me on this ...
Awaitng for positive response.

Regards,
Rohini G
Pune
Posted

you can use EPPLUS (free) to create the excel file
(you can download it with nuget)

you can make a generic method to retrieve the datasource
and use it both for grid generation and excel generation

OR

you can declare a ExcelPackage (the epplus excel file) and empty+fill every time you fill the grids
so your "export" button will only put a full file on the response.

Example :
http://epplus.codeplex.com/wikipage?title=WebapplicationExample[^]
 
Share this answer
 
What is the problem in that?
Single Gridview : Export Gridview Data to Excel in ASP.NET[^]

Multiple Gridview : MULTIPLE GRIDVIEW INTO MULTIPLE WORKSHEET[^]

     --Amy
 
Share this answer
 
Comments
rohini ghegade 31-Jul-14 5:23am    
Hi Amy,
Multiple Gridview into Multiple Worksheet link not getting Opened
can you plz copy paste the code here
CLient Side :
----------------------------------------

<pre lang="xml">&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
   &lt;div&gt;
   &lt;table&gt;
       &lt;tr&gt;
           &lt;td&gt;
               &lt;asp:GridView ID=&quot;gdvreport1&quot; runat=&quot;server&quot;&gt;
               &lt;/asp:GridView&gt;
           &lt;/td&gt;
       &lt;/tr&gt;
       &lt;br /&gt;&lt;br /&gt;
       &lt;tr&gt;
           &lt;td&gt;
               &lt;asp:GridView ID=&quot;grvreport2&quot; runat=&quot;server&quot;&gt;
               &lt;/asp:GridView&gt;
           &lt;/td&gt;
       &lt;/tr&gt;
       &lt;tr&gt;
           &lt;td&gt;
               &lt;asp:Button ID=&quot;btnget&quot; runat=&quot;server&quot; Text=&quot;GetData&quot; OnClick=&quot;btnget_Click&quot; /&gt;
           &lt;/td&gt;
           &lt;td&gt;
               &lt;asp:Button ID=&quot;btnexport&quot; runat=&quot;server&quot; Text=&quot;Export&quot; OnClick=&quot;btnexport_Click&quot; /&gt;
           &lt;/td&gt;
       &lt;/tr&gt;
   &lt;/table&gt;
   &lt;/div&gt;
   &lt;/form&gt;</pre>




Server Side :
-------------------------------------------

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MDIndia.ClassLibrary.ApplicationSupport;
using System.Data;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
using System.Text;
using System.Web.UI.HtmlControls;
 



public partial class NewTwo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnget_Click(object sender, EventArgs e)
    {
        try
        {


        }
        catch (Exception)
        {

            throw;
        }
    }
    private void GetconnectToAuth()
    {
        DBManager.CurrentDatabaseProvider = DatabaseProvider.MSSQLServer;
        DBManager.ConnectionString = "Data Source=MDICENTRAL\\AUTHORISATION;Initial Catalog=PIMS;User ID=mdindia1;Password=md!nd!@@123";
    }
    protected void btnexport_Click(object sender, EventArgs e)
    {

        GetconnectToAuth();
        string query1 = "Select ID,ICName from ICMaster";
        System.Data.DataTable dt = new System.Data.DataTable();
        dt = DBManager.SelectDataTable(query1);
        System.Data.DataTable dt2 = new System.Data.DataTable();
        query1 = "Select LabID,LabName from LabMaster";
        dt2 = DBManager.SelectDataTable(query1);
        dt2.TableName = "B";
        DataSet dataset = new DataSet();
        dataset.Tables.Add(dt.Copy());
        dataset.Tables.Add(dt2.Copy());


        Excel.Application excel = new Excel.Application();

        var workbook = (Excel._Workbook)(excel.Workbooks.Add(Missing.Value));

        for (var i = 0; i &lt; dataset.Tables.Count; i++)
        {
            if (workbook.Sheets.Count &lt;= i)
            {
                workbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing,
                                    Type.Missing);
            }

            var currentSheet = (Excel._Worksheet)workbook.Sheets[i + 1];

            switch (Convert.ToInt32(i))
            {
                case 0:
                    currentSheet.Name = "Projects";
                    break;
                case 1:
                    currentSheet.Name = "Employees";
                    break;


            }

            for (var j = 0; j &lt; dataset.Tables[i].Columns.Count; j++)
            {
                currentSheet.Cells[1, j + 1] = dataset.Tables[i].Columns[j].ToString();
            }

            for (var y = 0; y &lt; dataset.Tables[i].Rows.Count; y++)
            {
                for (var x = 0; x &lt; dataset.Tables[i].Rows[y].ItemArray.Count(); x++)
                {
                    currentSheet.Cells[y + 2, x + 1] = dataset.Tables[i].Rows[y].ItemArray[x];
                }
            }
        }
     
        workbook.SaveAs(Server.MapPath("Test.xlsx"), Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        workbook.Close(true, Type.Missing, Type.Missing);

        excel.Quit();

        var filePath = Server.MapPath("Test.xlsx");
  FileInfo targetFile = new System.IO.FileInfo(filePath);
 try
 {
 FileInfo objFileInfo = new FileInfo(filePath);
 Response.Clear();
 Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
 Response.AddHeader("Content-Disposition", ("attachment; filename=" + objFileInfo.Name));
 Response.AddHeader("Content-Length", objFileInfo.Length.ToString());
 Response.ContentType = "application/octet-stream";
 Response.WriteFile(filePath);
 Response.End();
 }
 catch (Exception ex)
 {
 }


    }
}</pre>
 
Share this answer
 
protected void btnexport_Click(object sender, EventArgs e)
    {

        GetconnectToAuth();
        string query1 = "Select ID,ICName from ICMaster";
        System.Data.DataTable dt = new System.Data.DataTable();
        dt = DBManager.SelectDataTable(query1);
        System.Data.DataTable dt2 = new System.Data.DataTable();
        query1 = "Select LabID,LabName from LabMaster";
        dt2 = DBManager.SelectDataTable(query1);
        dt2.TableName = "B";
        DataSet dataset = new DataSet();
        dataset.Tables.Add(dt.Copy());
        dataset.Tables.Add(dt2.Copy());


        Excel.Application excel = new Excel.Application();

        var workbook = (Excel._Workbook)(excel.Workbooks.Add(Missing.Value));

        for (var i = 0; i < dataset.Tables.Count; i++)
        {
            if (workbook.Sheets.Count <= i)
            {
                workbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing,
                                    Type.Missing);
            }

            var currentSheet = (Excel._Worksheet)workbook.Sheets[i + 1];

            switch (Convert.ToInt32(i))
            {
                case 0:
                    currentSheet.Name = "Projects";
                    break;
                case 1:
                    currentSheet.Name = "Employees";
                    break;


            }

            for (var j = 0; j < dataset.Tables[i].Columns.Count; j++)
            {
                currentSheet.Cells[1, j + 1] = dataset.Tables[i].Columns[j].ToString();
            }

            for (var y = 0; y < dataset.Tables[i].Rows.Count; y++)
            {
                for (var x = 0; x < dataset.Tables[i].Rows[y].ItemArray.Count(); x++)
                {
                    currentSheet.Cells[y + 2, x + 1] = dataset.Tables[i].Rows[y].ItemArray[x];
                }
            }
        }
     
        workbook.SaveAs(Server.MapPath("Test.xlsx"), Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        workbook.Close(true, Type.Missing, Type.Missing);

        excel.Quit();

        var filePath = Server.MapPath("Test.xlsx");
  FileInfo targetFile = new System.IO.FileInfo(filePath);
 try
 {
 FileInfo objFileInfo = new FileInfo(filePath);
 Response.Clear();
 Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
 Response.AddHeader("Content-Disposition", ("attachment; filename=" + objFileInfo.Name));
 Response.AddHeader("Content-Length", objFileInfo.Length.ToString());
 Response.ContentType = "application/octet-stream";
 Response.WriteFile(filePath);
 Response.End();
 }
 catch (Exception ex)
 {
 }


    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900