Sunday, September 23, 2012

Export to Excel with MVC

I’m sure you’ve all had to export to Excel at one point or another. I created a small wrapper around the EPPlus library for MVC. Basically my wrapper contains a few helpers for common formatting, an ActionResult, and a column definition. Here’s what the end result looks like in the simplest form:

Basically this exports all the columns and records in the SampleInfo collection. So if you want to specify the columns you want, you can do this:

The ExcelColumnDefinition.Create<> is kinda ugly, so you could create a small helper method for readability like this:

Now the above example looks like this:

So this is what the ExcelColumnDefinition looks like:

Here’s the actual ActionResult:

So that’s it. If you have any questions, please feel free to comment below.

You can browse or download the source on GitHub. The sample project has examples.

Shout it

kick it on

Related Posts Plugin for WordPress, Blogger...