How to export data from WPF DataGrid to Excel ?


  • I have to export data from WPF DataGrid to Excel using VSTS 2008.

    I am using stackpanel with datagrid to display data.I have to export data from DataGrid to excel on button click in WPF.

    Can anyone help me?

    Any help will be much appreciated.




    • Edited by Ram Pandey Monday, June 07, 2010 9:35 AM
    Monday, June 07, 2010 9:34 AM


All replies

  • You can use the following sample to create export functionality in DataGrid.

    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, June 07, 2010 9:52 AM
  • Monday, June 07, 2010 10:01 AM
  • Hi Ram,

    Could you let me know if Nayan and Mustehsan's answers helped?

    If you are still having problems, please let us know.

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Tuesday, June 15, 2010 8:13 AM
  • Hi,

    This code does not require Office libraries and end opens Microsoft Excel

            if (this.Contactos == null || this.Contactos.Count == 0)
            StringBuilder sb = new StringBuilder();
            // Titulos
            foreach (var item in typeof(ConsultarContactoSesionesResult).GetProperties())
              sb.AppendFormat(CultureInfo.CurrentUICulture, "{0}\t", item.Name);
            // Contenido
            foreach (var item in this.Contactos)
              foreach (var propiedad in typeof(ConsultarContactoSesionesResult).GetProperties())
                sb.AppendFormat(CultureInfo.CurrentUICulture, "{0}\t", propiedad.GetValue(item, null));
            // Grabar Archivo
            string path = string.Format("{0}\\Contactos_{1:yyyyMMdd}.xls",
            File.WriteAllText(path, sb.ToString());
            // Abrir Excel
            ProcessStartInfo obInfo = new ProcessStartInfo(path);
            obInfo.UseShellExecute = true;
            obInfo.WindowStyle = ProcessWindowStyle.Normal;
            using (Process obJob = new Process())
              obJob.StartInfo = obInfo;
          catch (Exception ex)
    Friday, November 05, 2010 5:35 AM
  • If your application is MVVM WPF app (and it should be MVVM app) then you can bind DataTable property from your view model to DataGrid.ItemsSource property and Delegate/Relay command property from your view model to button.Command property.

    When button is clicked, DelegateCommand will execute delegate, body of which could look something like this (using this Excel C# / VB.NET library):

    var ef = new ExcelFile();
    // Insert data table in worksheet, starting from worksheet's first row and column and include column headers
          ef.Worksheets.Add(dataTable.TableName).InsertDataTable(dataTable, 0, 0, true);
    ef.SaveXls(dataTable.TableName + ".xls");

    Friday, April 01, 2011 10:10 AM