Monday 14 April 2014

Export Data from Grid View to Excel 2007 in c#.net


In this article, I will explain how to export data from DataGridView to excel 2007 in C#.net.
-> To start a new windows form application click on New Project
-> Choose Visual C# in the installed templates list.
-> Choose the windows form application.
-> Name the new form DataGridView to Excel 2007 (or whatever you want).
-> Add DataGridView and ToolStrip from Toolbox.
-> Add some data into DataGridView at Design Time. 
     or
-> Design your form as bellow.



Before you create an Excel file you have to add the 2 reference Microsoft Excel 12.0 Object Library & Microsoft Office 12.0 Object Library to you windows form project.
-> To add a new Reference click Project -> Add Reference.
-> Reference Manager window will popup as bellow:



-> Navigate to the below References
     Microsoft Excel 12.0 Object Library
     Microsoft Office 12.0 Object Library
-> Click OK button
-> Now double click the export to excel 2007 button and write the following code

Code for Eport to excel 2007 button
try
            {
                Microsoft.Office.Interop.Excel.Application ExcelApp =
                new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook ExcelBook;
                Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;
                int i = 0;
                int j = 0;

                //create object of excel
                ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)
                ExcelApp.Workbooks.Add(1);
                ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)
                ExcelBook.ActiveSheet;

                //export header
                for (i = 1; i <= this.dataGridView1.Columns.Count; i++)
                {
                ExcelSheet.Cells[1, i] = this.dataGridView1.Columns[i -1].HeaderText;
                }

                //export data
                for (i = 1; i <= this.dataGridView1.RowCount; i++)
                {
                    for (j = 1; j <= dataGridView1.Columns.Count; j++)
                    {
                ExcelSheet.Cells[i + 1, j] = dataGridView1.Rows[i - 1].Cells[j-1].Value;
                    }
                }
                ExcelApp.Visible = true;

                //set font OS System to data range
                Microsoft.Office.Interop.Excel.Range myRange =
                ExcelSheet.get_Range(ExcelSheet.Cells[1, 1],
                ExcelSheet.Cells[this.dataGridView1.RowCount+1,
                this.dataGridView1.Columns.Count]);
                Microsoft.Office.Interop.Excel.Font x = myRange.Font;
                x.Name = "Arial";
                x.Size = 10;

                //set bold font to column header
                myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1],
                ExcelSheet.Cells[1, this.dataGridView1.Columns.Count]);
                x = myRange.Font;
                x.Bold = true;

                //autofit all columns
                myRange.EntireColumn.AutoFit();

                //
                ExcelSheet = null;
                ExcelBook = null;
                ExcelApp = null;
            }
            catch (Exception ex)
            {
                errorlbl.Text = "";
                errorlbl.Text = "Your Error Message Here " + ex.Message;
                errorlbl.Visible = true;
            }        

Press F5 to Test your Program
Once you click on Export to Excel button you will see data in excel file like as shown below


Happy Programming








Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments:

Post a Comment