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".

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

3. Next we will load the data into dataGridView1 using the method:
LoadGridData();
The LoadGridData() method is here:
- private void LoadGridData()
- {
- DataTable dt = new DataTable();
- dt.Columns.Add("EmpNo.", typeof(int));
- dt.Columns.Add("Emp.Name", typeof(string));
- dt.Columns.Add("Hiredate", typeof(DateTime));
- dt.Columns.Add("Department", typeof(int));
- dt.Columns.Add("Salary", typeof(double));
- DataRow dr = dt.NewRow();
- dr["EmpNo."] = 1;
- dr["Emp.Name"] = "Vinoth";
- dr["Hiredate"] = DateTime.Now;
- dr["Department"] = 20;
- dr["Salary"] = 15000;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["EmpNo."] = 2;
- dr["Emp.Name"] = "Krishnan";
- dr["Hiredate"] = DateTime.Now.AddMinutes(1);
- dr["Department"] = 30;
- dr["Salary"] = 17000;
- dt.Rows.Add(dr);
- dataGridView1.DataSource = dt;
- }
And finally assigned the DataTable (dt) to dataGridView1 using the DataSource property.

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

5. Now we create an Object for the Excel Application using the ApplicationClass.
- Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
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:
- for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
- {
- XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
- }
8. Next, we loop through the rows and columns of the dataGridView1 to store the values in Excel.
- 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();
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.

Here is the Source Code of the project:
- 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 ExportToExcel
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- LoadGridData();
- }
- private void LoadGridData()
- {
- DataTable dt = new DataTable();
- dt.Columns.Add("EmpNo.",typeof(int));
- dt.Columns.Add("Emp.Name",typeof(string));
- dt.Columns.Add("Hiredate",typeof(DateTime));
- dt.Columns.Add("Department",typeof(int));
- dt.Columns.Add("Salary",typeof(double));
- DataRow dr = dt.NewRow();
- dr["EmpNo."] = 1;
- dr["Emp.Name"] = "Vinoth";
- dr["Hiredate"] = DateTime.Now;
- dr["Department"] = 20;
- dr["Salary"] = 15000;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["EmpNo."] = 2;
- dr["Emp.Name"] = "Krishnan";
- dr["Hiredate"] = DateTime.Now.AddMinutes(1);
- dr["Department"] = 30;
- dr["Salary"] = 17000;
- dt.Rows.Add(dr);
- dataGridView1.DataSource = dt;
- }
- private void btn_Cancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btn_Export_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;
- }
- }
- }
- }
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.

ADITYA SHUKLAPosted Jul 26, 2019, 1:00 AM
Error CS0119: 'NullReferenceException' is a type, which is not valid in the given context
Dharmendra Kumar PanditPosted Feb 2, 2019, 3:40 AM
Working Fine.
yaser alshikhPosted Sep 20, 2017, 4:38 AM
Thanks. Very nice, simple, and helpful example.
Salman MushtaqPosted Aug 19, 2017, 2:44 PM
Hello, I Face error "Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead." How to resolve this issue? And Also this code give exception.
SubashPosted Sep 6, 2016, 2:45 AM
Nice one
Former memberPosted Nov 13, 2013, 2:09 AM
You can also do that with Aspose.Cells for .NET Library : http://www.aspose.com/.net/excel-component.aspx
Kiran Kumar TalikotiPosted Oct 17, 2013, 11:25 AM
Nice article.Helped a lot..
Gakenh01Posted Aug 5, 2013, 7:59 AM
Thanks. Very nice, simple, and helpful example. I might try it out for a tough report I have coming up.