tags:

views:

1655

answers:

4

How can I export a GridView's data to a Microsoft Excel 2007 file?

Also, does Microsoft Excel 2007 support html elements and tags?

+1  A: 

I have used Aspose.Cells to do this. This component has a mechanism to programmatically create an Excel document from a GridView. I usually have a LinkButton whose click event handler constructs the document and writes the data into the Response.

tvanfosson
+3  A: 

If your users are using IE, just have them right-click on the GridView (which will have been rendered as an HTML table), right-click, and select "Export to Excel." It isn't an amazing faeture--and it requires IE--but otherwise if you need quick and dirty HTML table export to Excel is works fine--and you can tell 'em you worked all night writing lots of code to make it work!

rp
+2  A: 

You can create a page with the same information in a plain old table and then change the content type of the page to Excel. This might work with a GridView as well, but you should minimize any other controls or HTML as they will try to render them in Excel. Maybe have a button that goes to a page with just the grid or the equivalent table. The excel file might be funky cause of the HTML, but it works.

Here is the code to cange the content type.

Response.ContentType = "application/vnd.ms-excel";

If you want to force a download popup so they can save the file, this is the code:

Response.AppendHeader("Content-disposition", "attachment; filename=my.xls");
Charles Graham