Tuesday, 29 May 2012

ASP.NET list of objects to excel

I needed a simple export to excel in .NET without using the Microsoft Interop DLL and found a neat way to export from a gridview from the code behind.

The code is simple and generates a gridview on the fly and outputs to the HttpResponse:


To call the method in my code behind .cs file all I need is:
Export(Response, fileName, List<T>);
For the code I was writing I had to implement a DTO as by default the list of objects are exported in the order of member variables within the class so needed reordering and excluding as the standard object included ids etc.

No comments:

Post a Comment