(616) 371-1037

[email protected]

A simple, generic Excel file exporter

May 2, 2018 - John Waters


I recently needed to write a routine that exports a query to an Excel file, and came up with a generic solution I thought might be helpful to share. The code is in an AspNetCore project, but the same principles would work in other .NET scenarios.

Let’s start with the signature of the export method:

The output is a byte[], which is the actual contents of the Excel file (an xlsx file). I end up returning this from my API controller as a FileResult, using:

Here, fileBytes is the result from my export. By supplying the correct MIME type and a filename, the Angular web app downloading this ends up just dropping a nicely named Excel file in your Downloads folder.

Going back to the method declaration, the first parameter is a List<T> of data rows. You can pass in whatever list of data you want. In my case, I am passing the result of an EntityFrameworkCore query:

You’ll notice that the select statement is using an anonymous type (new { …. ). I can still pass the list to my exporter func… I dont have to declare the type in code. In the anonymous type, some of the assignments specify a property name, for instance TripDate = t.Date, others don’t, for instance t.EstimatedTravelTime. When a name is specified, the anonymous type uses that property name, otherwise the  assigned identifier.

The generic exporter will use these property names as the headers in the first row of the Excel file, but I also allow you to supply a custom list of headers to use in the second optional parameter of the call. The exporter will write the values of each property to the file, using whatever default formatting that gives you. If you want to be in more control, you can specify a function that does the formatting. In my code, I supply both:

Here, FormatTime is a local function that converts a nullable Double representing time in seconds to a more pleasant hours and minutes string:

The result of this formatting is an Excel file that looks something like this:

OK – now that we have seen the signature of the method, what I pass in, and the results… let’s delve into the magic in the black box!

First of all, I am using the excellent https://www.nuget.org/packages/EPPlus

This package makes reading and writing Excel (XLSX) files a breeze. The first few rows of code in this method just create a new Worksheet called “Export”. I use a MemoryStream as the ‘backing storage’ for writing the file in memory. The rows and columns both start at 1, so I set that up.

Now I need to write the header row. If I supplied headers, I use those, otherwise I get the names of the properties of the data type  using typeof(T).GetProperties(), and pis.Select( pi => pi.Name).

Now I need to iterate over the data and write it out. For each row, I either use the raw property values, using pis.Select( pi => pi.GetValue(0)), or I pass the row to the supplied formatter function, which returns an array of objects to use instead.

Finally, I Auto Fit all the columns to the data in them, and return the file contents as an array of bytes:

Putting this all together:

So very little code, and remarkably flexible!

I hope you find this useful in your projects.



John Waters

2 thoughts on “A simple, generic Excel file exporter

  • Thiago

    October 18, 2018 at 6:17 am

    Hi John! That’s awesome and a clean solution, this is exactly i’ve been looking for. Would you mind i use this code in my project?
    Thanks =)


Leave a comment

Your email address will not be published. Required fields are marked *