My blog has moved!

Visit http://blog.adamroderick.com/ and update your bookmarks.

Thursday, July 15, 2010

Download to Excel Behavior for Silverlight DataGrid

I wrote a behavior to export a DataGrid to Excel.  Silverlight allows saving files, and this behavior transforms a DataGrid into a .csv file.

To use, add a reference to your project to the assembly, and you will see the DownloadToExcel behavior under the Assets tab:

assets

Add the behavior to any UIElement.  In this case, I added it to a button:

addedBehavior

The default event selected on the button is the Click event, which is what we want.  Set the Target Object by clicking on the little target icon and then choosing the data grid you want to export to excel:

chooseTarget

Below is a screenshot of the test app.  When we run it and click the button, we are prompted to save the file.  This is the DataGrid in the sample code:

datagrid

The resulting .CSV file--which opens in Excel by default--it looks like this:

excel

 

Download just the .DLL or the source code with sample app.

Thanks to stackoverflow and those who responded to this question for giving me insight.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home