List<ExpandoObject> to Excel Export

February 4, 2013

ASP.NET, C#, Learning, Nerd, Programming

I’m writing this to share a quick solution I implemented to solve a need on a small project.

It works, but feels dirty. If you have a cleaner reco, PLEASE let me know.

Handler:

(My apologies for screenshots. Git Gists were not keeping the formatting when I selected c#)

image

 

Extension Method:

image

HEADS UP

This happens due to Excel settings that can be changed if desired on the client, but opens fine:

image

 

 

TL;DR

REASON I DID THIS:

I wrote a Lync method that pulls the latest, active permissions per user email. Because of this, each email address row could have x permissions columns, thus becoming a collection of type dynamic (IEnumerable<dynamic>). New perms can always be added, so options are dynamic (and this db has no lookup table for them).

Don’t judge :-) But if you can constructively advise, I’d love to hear it (if there is a cleaner way or if this isn’t as dirty as it made me feel).

Thanks! Posting in hopes it helps someone. Or if this doesn’t help, maybe a comment will ;-)

, , ,

2 Comments on “List<ExpandoObject> to Excel Export”

  1. Ivan Towlson Says:

    Hi Cori,

    Nitpick: You’re not actually using the dynamicness of the items in the extension method. It would be more self-documenting to change the extension method to take an IEnumerable<IDictionary> — you will still be able to pass it a List because ExpandoObject is IDictionary. Alternatively, if you really want to support arbitrary dynamic objects, you could change the implementation to query the object for the set of dynamic properties — for ExpandoObject that would be the same as the dictionary, but it would also work for objects that don’t expose their properties as a dictionary. See IDynamicMetaObjectProvider if you want to do this (and stand by to enjoy a near-complete absence of documentation *sigh*).

    Reply

  2. Isxek Says:

    Try posting your source code using this: http://en.support.wordpress.com/code/posting-source-code/

    It has built-in support for C# code, and there are other options you can tweak if needed.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: