tags:

views:

4820

answers:

3

I am working on an ASP.NET MVC application where I need to export data to an excel spreadsheet. Previously, in webforms apps, I used some code I found to render a GridView as an excel-compatible file. This was quite handy. I was wondering what the quickest/most effective method would be to do this in MVC. Thanks.

+6  A: 

One simple option would be to create a view to render an XML-version of an Excel File. You could either use the new Office 2007 version, or the older 2003 version. We chose the 2003 version so that more people could use it, but that's up to you, of course.

XML 2003 ref on MSDN

XML 2007 ref on MSDN

sgwill
I've used the same strategy to render RSS feeds, etc. Works great.
GalacticCowboy
We're using it several places now - web project creates a template for the end user so that we can re-import that file knowing that the format is correct. XML format is great!
sgwill
A: 

Check out this @ CodeProject.

Bruno Shine
Thanks but I dont think this would be good to use in an MVC app because it is a server control.
BigJoe714
+11  A: 

Here is a blog post from Stephen Walther entitled ASP.NET MVC Tip #2 - Create a custom Action Result that returns Microsoft Excel Documents

BigJoe714
This uses the old trick of writing an HTML document (basically a table) with an Excel mime type. This is different than streaming a native Excel file. That said, this is a fine way to do it for most application, since Excel handles HTML formatting pretty well.
Tim Scott
It's fairly easy to change this to rendering as CSV if you don't need any fancy formatting. Just remember to double-quote all the fields if there's a possibility of commas showing up.
Andrew Lewis