HOW TO EXPORT DATA FROM GRIDVIEW TO EXCEL FILE USING C# WINDOWS APPLICATION



HOW TO EXPORT DATA FROM GRIDVIEW TO EXCEL FILE USING C# WINDOWS APPLICATION
                         In this post, I am going to explain and demonstrate how to export data from DataGridView to Excel file using C# Windows application.

1. Design your form by placing a DataGridView control on it. Here I have designed it like this:





2. Next we will load the data into dataGridView1 using the method:
         public void LoadGridData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Address");
            dt.Columns.Add("Mobile");
            dt.Rows.Add("Ravi Kumar Soni""Transport Department""82690884xx");
            dt.Rows.Add("Vikash Singh Chandel""NIC""82690482xx");
            dataGridView1.DataSource = dt;
        }

3. And finally assign values in data table values looks like this:




4. Next we add a reference to the Microsoft.Office.Interop DLL to our project from "Project" -> "Add Reference".



5. Double click on Export to excel button and write this code

if (dataGridView1.Rows.Count > 0)
            {
               Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
               XcelApp.Application.Workbooks.Add(Type.Missing);

                for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                {
                    XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
                }

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    }
                }
                XcelApp.Columns.AutoFit();
                XcelApp.Visible = true;

Final Complete Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExportDateFromGridViewToExcel
{
    public partial class Form1 : Form
    {
        DataTable dt = new DataTable();
        public static string FileName = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            LoadGridData();
        }
        public void LoadGridData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Address");
            dt.Columns.Add("Mobile");
            dt.Rows.Add("Ravi Kumar Soni""Transport Department""82690884xx");
            dt.Rows.Add("Vikash Singh Chandel""NIC""82690482xx");
            dataGridView1.DataSource = dt;
        }
        private void button1_Click(object sender, EventArgs e)
        {

            if (dataGridView1.Rows.Count > 0)
            {
                Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                XcelApp.Application.Workbooks.Add(Type.Missing);

                for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                {
                    XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
                }

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    }
                }
                XcelApp.Columns.AutoFit();
                XcelApp.Visible = true;
            }
        }
    }
}

6.  Run Code and click on Export to button.








download source Code: Click Here

written by Ravi Kumar Soni

No comments:

Post a Comment