HOW TO EXPORT DATA FROM DATAGRIDVIEW TO EXCEL IN C#.NET WINDOWS APPLICATION.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  Excel 
C#
 2205  View(s)
Ratings:
Rate this:
I want to export data from datagridview to excel in c#.net windows application.how to do this please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to export data from datagridview to excel in c#.net windows application.

    I will explain here how to export data from datagridview to excel in c# windows application.
    Show below Code and excel formate.

    using System;
    using System.Windows.Forms;
    using System.IO;
    using System.Data;
    using System.Data.OleDb;
    using Excel = Microsoft.Office.Interop.Excel;
    namespace export_Excel
    {
        public partial class Form1 : Form
        {
           
            public Form1()
            {
                InitializeComponent();
            }      
        
            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                Excel.Application xelApp;
                Excel.Workbook xelWorkBook;
                Excel.Worksheet xelWorkSheet;
                object misingValue = System.Reflection.Missing.Value;

                xelApp = new Excel.Application();
                xelWorkBook = xelApp.Workbooks.Add(misingValue);
                xelWorkSheet = (Excel.Worksheet)xelWorkBook.Worksheets.get_Item(1);
                int i = 0;
                int j = 0;

                for (i = 0; i <= GridView1.RowCount - 1; i++)
                {
                    for (j = 0; j <= GridView1.ColumnCount - 1; j++)
                    {
                        DataGridViewCell cell = GridView1[j, i];
                        xelWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                    }
                }

                xelWorkBook.SaveAs("Employee.xls", Excel.XlFileFormat.xlWorkbookNormal, misingValue, misingValue, misingValue, misingValue, Excel.XlSaveAsAccessMode.xlExclusive, misingValue, misingValue, misingValue, misingValue, misingValue);
                xelWorkBook.Close(true, misingValue, misingValue);
                xelApp.Quit();

                Objectreleased(xelWorkSheet);
                Objectreleased(xelWorkBook);
                Objectreleased(xelApp);

                MessageBox.Show("Excel file created , you can see the file E:\\Employee.xls");
            }
            private void Objectreleased(object obj)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                    obj = null;
                }
                catch (Exception ex)
                {
                    obj = null;
                    MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }

       
        }
    }

    Result

     

      Modified On Jan-07-2016 11:57:13 PM
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to export data from datagridview to excel in c#.net windows application.

    Post is removed by the Admin.

  1. Copbel Beldi

    Post:1

    Points:7
    Re: how to export data from datagridview to excel in c#.net windows application.

    There are many options for exporting Excel file. One of them is to use an Excel library like EasyXLS.


    The code is below:

    <code>

    ExcelDocument workbook = new ExcelDocument(1);
               
    ExcelWorksheet worksheet = (ExcelWorksheet)workbook.easy_getSheetAt(0);
    ExcelTable table = worksheet.easy_getExcelTable();

    for (int column = 0; column < dataGridView.Columns.Count; column++)
    {
        table.easy_getCell(0, column).setValue(dataGridView.Columns[column].HeaderText);
    }

    for (int row = 0; row < dataGridView.Rows.Count-1; row++)
    {
        for (int column = 0; column < dataGridView.Columns.Count; column++)
        {
            xlsTable.easy_getCell(row+1, column).setValue(dataGridView.Rows[row].Cells[column].Value.ToString());
         }
    }

    workbook.easy_WriteXLSXFile("file.xlsx");

    </code>


    For details about exporting other items like colors, fonts, see export datagridview to Excel in C# from EasyXLS website.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!