Export DataGridView Data to Excel in C# Without Saving to File System

This article explains how to export DataGridView data to Excel in C# without saving to a Local File System.

1. Create a new project in Visual C# and select "Windows Forms Application" from the list of available templates and name your project "ExportToExcel".

Excel1.jpg

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

Excel2.jpg

3. Next we will load the data into dataGridView1 using the method:

LoadGridData();

The LoadGridData() method is here:

  1. private void LoadGridData()  
  2. {  
  3.     DataTable dt = new DataTable();  
  4.     dt.Columns.Add("EmpNo."typeof(int));  
  5.     dt.Columns.Add("Emp.Name"typeof(string));  
  6.     dt.Columns.Add("Hiredate"typeof(DateTime));  
  7.     dt.Columns.Add("Department"typeof(int));  
  8.     dt.Columns.Add("Salary"typeof(double));  
  9.     DataRow dr = dt.NewRow();  
  10.     dr["EmpNo."] = 1;  
  11.     dr["Emp.Name"] = "Vinoth";  
  12.     dr["Hiredate"] = DateTime.Now;  
  13.     dr["Department"] = 20;  
  14.     dr["Salary"] = 15000;  
  15.     dt.Rows.Add(dr);  
  16.     dr = dt.NewRow();  
  17.     dr["EmpNo."] = 2;  
  18.     dr["Emp.Name"] = "Krishnan";  
  19.     dr["Hiredate"] = DateTime.Now.AddMinutes(1);  
  20.     dr["Department"] = 30;  
  21.     dr["Salary"] = 17000;  
  22.     dt.Rows.Add(dr);  
  23.     dataGridView1.DataSource = dt;  
  24. }  
Here we have created an instance of the DataTable class and added the columns EmpNo., Emp.Name, Hiredate, Department and Salary. Since each row in a DataTable is represented by a DataRow we have created the DataRow object and added the DataRow dr to DataTable using the Add method.

And finally assigned the DataTable (dt) to dataGridView1 using the DataSource property.

Excel3.jpg

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

Excel4.jpg

5. Now we create an Object for the Excel Application using the ApplicationClass.

  1. Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();  
6. Next, we create a new workbook using the Add method of the Object:

XcelApp.Application.Workbooks.Add(Type.Missing);

7. Now, we need to store the Header details for the DataGridView in Excel using code as in the following:
  1. for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)  
  2. {  
  3.     XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;  
  4. }  

 

8. Next, we loop through the rows and columns of the dataGridView1 to store the values in Excel.

  1. for (int i = 0; i < dataGridView1.Rows.Count; i++)  
  2. {  
  3.     for (int j = 0; j < dataGridView1.Columns.Count; j++)  
  4.     {  
  5.         XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();  
  6.      }  
  7. }  
9. Here, next we set the properties of the Workbook column to AutoFit:

XcelApp.Columns.AutoFit();

10. Then we open Excel with the workbook using the Visible property as in the following:

XcelApp.Visible = true;

11. Move to the Designer and click the Export button. Now, a new Excel workbook is opened with the DataGridView details in it.

Excel5.jpg

Here is the Source Code of the project:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace ExportToExcel  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void Form1_Load(object sender, EventArgs e)  
  18.         {  
  19.             LoadGridData();  
  20.         }  
  21.         private void LoadGridData()  
  22.         {  
  23.             DataTable dt = new DataTable();  
  24.             dt.Columns.Add("EmpNo.",typeof(int));  
  25.             dt.Columns.Add("Emp.Name",typeof(string));  
  26.             dt.Columns.Add("Hiredate",typeof(DateTime));  
  27.             dt.Columns.Add("Department",typeof(int));  
  28.             dt.Columns.Add("Salary",typeof(double));  
  29.             DataRow dr = dt.NewRow();  
  30.             dr["EmpNo."] = 1;  
  31.             dr["Emp.Name"] = "Vinoth";  
  32.             dr["Hiredate"] = DateTime.Now;  
  33.             dr["Department"] = 20;  
  34.             dr["Salary"] = 15000;  
  35.             dt.Rows.Add(dr);  
  36.             dr = dt.NewRow();  
  37.             dr["EmpNo."] = 2;  
  38.             dr["Emp.Name"] = "Krishnan";  
  39.             dr["Hiredate"] = DateTime.Now.AddMinutes(1);  
  40.             dr["Department"] = 30;  
  41.             dr["Salary"] = 17000;  
  42.             dt.Rows.Add(dr);  
  43.             dataGridView1.DataSource = dt;  
  44.         }  
  45.         private void btn_Cancel_Click(object sender, EventArgs e)  
  46.         {  
  47.             this.Close();  
  48.         }  
  49.         private void btn_Export_Click(object sender, EventArgs e)  
  50.         {  
  51.             if (dataGridView1.Rows.Count > 0)  
  52.             {  
  53.                 Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();  
  54.                 XcelApp.Application.Workbooks.Add(Type.Missing);  
  55.                 for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)  
  56.                 {  
  57.                     XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;  
  58.                 }  
  59.                 for (int i = 0; i < dataGridView1.Rows.Count; i++)  
  60.                 {  
  61.                     for (int j = 0; j < dataGridView1.Columns.Count; j++)  
  62.                     {  
  63.                         XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();  
  64.                     }  
  65.                 }  
  66.                 XcelApp.Columns.AutoFit();  
  67.                 XcelApp.Visible = true;  
  68.             }  
  69.         }  
  70.     }  
  71. }  

 

Conclusion

In this article we discussed how to export data from DataGridView to Excel using Microsoft.Office.Interop namespace without saving to the file system.


Similar Articles