Creating an Excel document without excel installed … in one line of code

Yes, we can: code a complete excel document in only one line of code. The secret: the fluent interface of Linq2XML. Excel document are (since Excel 2003) XML documents, so you can create them without having Excel installed – e.g. on a web-server, where you definitely don’t want to have Com-Interop and a running Excel process.

In my project Sem.Sync I’m writing excel documents containing flatened contact information using this approach. have a look at the following code to see how easy it is:

You will need to fix the last line to include “?xml version=\”1.0\”?” between the opening and closing angle-bracket.

The GetPropertyList method creates a list of strings with “paths” to the properties and GetPropertyValueString gets a string representation of such properties from an object. As you can see the rows and cells are created inside a Linq-expression and you can compile the excel document in really just one line of code – but you should think about maintainence, if you do so ;-).