Home > DeveloperSection > Forums > how to export data from datagridview to excel in c#.net windows application.
Ankit Singh

Total Post:341

Points:2389
Posted on    January-07-2016 11:47 PM

 C# C#  Excel 
Ratings:


 2 Reply(s)
 583  View(s)
Rate this:
I want to export data from datagridview to excel in c#.net windows application.how to do this please help me.


aditya kumar Patel

Total Post:242

Points:1722
Posted on    January-07-2016 11:49 PM

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

aditya kumar Patel

Total Post:242

Points:1722
Posted on    January-08-2016 12:54 PM

Post is removed by the Admin.

Don't want to miss updates? Please click the below button!

Follow MindStick