Export WPF Datagrid To Excel

Hello friends here I am sharing how to export wpf datagrid  data to excel.

In my wpf form I have datagrid and button to export in excel as show in below image.


Now my datagrid look as below image with state data.


Now on export button click event first open savefile dialog and check for dialog result and select all cells of datagrid and use streamwriter object of system.IO to write file as below code:

private void btn_exprt_Click(object sender, RoutedEventArgs e)


Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

dlg.Filter = “Excel File|*.xls;*.xlsx;*.xlsm”;

Nullable<bool> Dialogresult = dlg.ShowDialog();

if (Dialogresult == true)



YourDatagrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;

YourDatagrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;

ApplicationCommands.Copy.Execute(null, YourDatagrid);

String resultat = (string)Clipboard.GetDat(DataFormats.CommaSeparatedValue);

String result = (string)Clipboard.GetData(DataFormats.Text);


System.IO.StreamWriter file = new System.IO.StreamWriter(dlg.FileName);





Now click on button and select location to save file and give file name and click save button as given image.


Goto selected location the excel file is created as show in below image.


Excel file is ready, open excel !!!!!.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s