.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Export datagridview to Excel in c#

Posted By:Hiren Soni       Posted Date: August 18, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

here i will show you how to export datagridview to excel
 

Subject: Export DataGridView to Excel in C#

 

First add the reference from the com tab, and add Microsoft Excel 12.0 object library.

 

Code:

     private void button1_Click_1(object sender, EventArgs e)

        {

 

            // creating Excel Application

            Microsoft.Office.Interop.Excel._Application app  = new Microsoft.Office.Interop.Excel.Application();

 

 

            // creating new WorkBook within Excel application

            Microsoft.Office.Interop.Excel._Workbook workbook =  app.Workbooks.Add(Type.Missing);

           

 

            // creating new Excelsheet in workbook

             Microsoft.Office.Interop.Excel._Worksheet worksheet = null;                   

           

           // see the excel sheet behind the program

            app.Visible = true;

          

           // get the reference of first sheet. By default its name is Sheet1.

           // store its reference to worksheet

            worksheet = workbook.Sheets["Sheet1"];

            worksheet = workbook.ActiveSheet;

 

            // changing the name of active sheet

            worksheet.Name = "Exported from gridview";

 

           

            // storing header part in Excel

            for(int i=1;i

            {

    worksheet.Cells[1, i] = dataGridView1.Columns[i-1].HeaderText;

            }

 

 

 

            // storing Each row and column value to excel sheet

            for (int i=0; i < dataGridView1.Rows.Count-1 ; i++)

            {

                for(int j=0;j

                {

                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

                }

            }

 

 

            // save the application

            workbook.SaveAs("c:\\output.xls",Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive , Type.Missing, Type.Missing, Type.Missing, Type.Missing);

           

            // Exit from the application

            app.Quit();

 

           

      

   

        }

 

   

 

 

Note :

In this part of code:

            // storing Each row and column value to excel sheet

            for (int i=0; i < dataGridView1.Rows.Count-1 ; i++)

            {

                for(int j=0;j

                {

                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

                }

            }

 

I have taken dataGridView1.Rows.Count-1, because in datagridview it contains empty row at the last. (See in the figure of datagridview.)

 

 

Hope you understand it.

 

Thank you.



 Subscribe to Articles

     

Further Readings:

Responses
Author: CikaPero         Company URL: http://www.dotnetspark.com
Posted Date: September 27, 2010

Hi,

much faster solution for <a href="http://www.gemboxsoftware.com/LA/ASP-.NET-Excel.htm">Excel ASP.NET</a> application is this <a href="http://www.gemboxsoftware.com/GBSpreadsheet.htm">Excel C#</a> component that doesn't use <a href="http://www.gemboxsoftware.com/LA/Excel-Automation-and-Excel-Interop.htm">Excel Interop</a>. I recommend you take a look at it.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend