Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

Exporting Data Grid to Excel with Save Dialog Box in C#.NET

Rate me:
Please Sign up or sign in to vote.
4.73/5 (14 votes)
9 Oct 2014CPOL 39.6K   14   10
How to export Data Grid to Excel with Save Dialog Box in C#.NET

Introduction

I’ve the following data grid (say comparisonGrid) and I'll export this data into Excel by following the below steps:

Step 1

Include the following namespace to your code behind file (.cs file):

C#
using Excel = Microsoft.Office.Interop.Excel;

Step 2

Now you would need to instantiate the instances of Excel application, Excel workbook and worksheet as below (I'll do that in the export button click event):

C#
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

Step 3

Fill in the Excel sheet with the values of the data grid (cell by cell) as follows:

C#
for (int i = 0; i <= comparisonGrid.Items.Count - 1; i++)
{
	for (int j = 0; j <= comparisonGrid.Columns.Count - 1; j++)
	{
		xlWorkSheet.Cells[i + 1, j + 1] =  
		( (DataRowView)comparisonGrid.Items[i]).Row.ItemArray[j].ToString();
	}
}

Step 4

For saving the file using “Save dialog” box, you would add the following code:

C#
System.Windows.Forms.SaveFileDialog saveDlg = new System.Windows.Forms.SaveFileDialog();
saveDlg.InitialDirectory = @"C:\";
saveDlg.Filter = "Excel files (*.xls)|*.xls";
saveDlg.FilterIndex = 0;
saveDlg.RestoreDirectory = true;
saveDlg.Title = "Export Excel File To";
if (saveDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
	string path = saveDlg.FileName;
	xlWorkBook.SaveCopyAs(path);
	xlWorkBook.Saved = true;
	xlWorkBook.Close(true, misValue, misValue);
	xlApp.Quit();
}

You may encounter the below error:

Error 1 Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded.
Use the applicable interface instead.

Fix

In your Project, expand the "References", find the Microsoft Office Interop reference. Right click it and select properties, and change "Embed Interop Types" to false. Add the following reference:

C#
using Excel = Microsoft.Office.Interop.Excel;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
For more technical articles please visit my blog at-
http://komalmangal.blogspot.in

Comments and Discussions

 
Questiongood one Pin
Member 1227488627-Jan-16 1:29
Member 1227488627-Jan-16 1:29 
QuestionHow to exclude the image column from exporting to excel Pin
Member 1205998930-Oct-15 3:45
Member 1205998930-Oct-15 3:45 
QuestionMy Vote 2 Pin
M.Farrukh Abbas15-Oct-14 1:54
M.Farrukh Abbas15-Oct-14 1:54 
QuestionBetter to try with OLEDB Pin
NaibedyaKar13-Oct-14 2:14
professionalNaibedyaKar13-Oct-14 2:14 
AnswerRe: Better to try with OLEDB Pin
Komal Mangal13-Oct-14 2:27
Komal Mangal13-Oct-14 2:27 
GeneralRe: Better to try with OLEDB Pin
NaibedyaKar13-Oct-14 2:28
professionalNaibedyaKar13-Oct-14 2:28 
Perhaps you could have both Smile | :)
GeneralMy vote of 1 Pin
Yks10-Oct-14 20:56
Yks10-Oct-14 20:56 
QuestionRegarding "Microsoft.Office.Interop.Excel" Reference Pin
Raghvendra2410-Oct-14 18:36
professionalRaghvendra2410-Oct-14 18:36 
GeneralMy vote of 1 Pin
Mike Tuersley10-Oct-14 9:18
Mike Tuersley10-Oct-14 9:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.