Solution 4 : Export Silverlight DataGrid to Excel XML

While ASP.NET GridView control supports data export to Excel, Silverlight DataGrid control does not. To implement the data export, you need to retrieve data from the DataGrid and write the data to an Excel XML file (Office Open XML File Format).

1.       Use Microsoft Excel application to create a new Excel spreadsheet with the format that you want for the data export. Enter a couple of dummy data and Save As XML spreadsheet. This is your Excel template.

2.      Copy all the source of the above Excel template to where you will develop the data export function such as in the ExportToExcelButton_Click event. The following are partial source.

3.      Use Silverlight SaveFileDialog control to create a StreamWriter instance. Call WriteLine method of the  instance in each line of the copied Excel template source.

4.      Running the code now, you should be able to export data to an Excel XML file, which is identical to the one you created originally in the step 1. You can use this to verify that your base code is working.

5.      Replace data-driven lines with dynamic data.

6.      Retrieve data from ItemsSource of the DataGrid and replace the dummy data lines with the retrieved data. (Assume entity class of Customer with single property of Name).


Comments have been disabled for this content.