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.
sgwill
2008-11-19 14:19:15
I've used the same strategy to render RSS feeds, etc. Works great.
GalacticCowboy
2008-11-19 14:50:03
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
2008-11-19 16:01:41
Thanks but I dont think this would be good to use in an MVC app because it is a server control.
BigJoe714
2008-11-19 14:45:11
+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
2008-11-19 16:34:23
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
2008-11-19 20:08:19
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
2010-03-15 21:35:18