ComponentOne Grid for WPF: Grid for WPF Task-Based Help > Exporting Data to an Excel File

Exporting Data to an Excel File

Starting with the 2009 v3 release, you can export the content of your grid application to a Microsoft Excel file.

In this topic you'll add a button to the project that, when clicked, will export the grid's content. You'll use the ExportToExcel method to export grid content, and you'll change the export's settings using ExcelExportSettings.

Complete the following steps:

1.   Complete steps 1 and 2 of the Grid for WPF Quick Start to create a new WPF project, add a C1DataGrid control to the project, and bind the project to a data source. (Note that you can complete the following with an unbound C1DataGrid control, but you may need to adapt the following steps.)

2.   From the Toolbox add a Button control to the window, resize it, and, in the Properties window, set Button1's Content property to "Export".

3.   Double-click Button1 to create the Button1_Click event handler and switch to Code view.

4.   Add code to the Button1_Click event handler to call the ExportToExcel method:

      Visual Basic

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

    ' Customize export settings.

    Dim ExportSet as New ExcelExportSettings

    Me.ExportSet.FileFormat = ExcelFormat.Excel97

    Me.ExportSet.IncludeColumns.Add("ProductName")

    ' Export data to the selected file.

    Me.C1DataGrid1.ExportToExcel("c:\products.xls", ExportSet)

End Sub

      C#

private void Button1_Click(object sender, System.EventArgs e)

{

    // Customize export settings.

    ExcelExportSettings ExportSet = new ExcelExportSettings();

    this.ExportSet.FileFormat = ExcelFormat.Excel97;

    this.ExportSet.IncludeColumns.Add("ProductName");

    // Export data to the selected file.

    this.c1DataGrid1.ExportToExcel(@"c:\products.xls", ExportSet);

}

Run your program and observe:

Click the Export button. The ProductName column will be saved as a Microsoft Excel 97/2003 file named products.xls  in the C:\ directory.


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.