Generating CSV files with .NET using the Razor Engine

There comes a time in almost every business related application, where a client has a requirement for you to have your data export into CSV.

Well, simple, I hear you say - I'll just make this amazing contraption with .NET Reflection that goes through my class - checks what properties they have, and serializes that data into a CSV.

All good, and you send your beautifully elegantly produced CSV to the client, and they respond with: "Well, that looks good, but I don't want that bit of the data, and could you format this data in this way, and do this and that".

So here's my solution - a solution that I've really liked, and have used succesfully and that delivers on both separating the actual CSV-generation from the code and not having to deal with string replacements or regex or anything. Also, if your client wishes to change the implementation, as they always do - you'll be fine.

On with the the how to:

Fire up Visual Studio, and install through Tools -> Extensions and Udpates...an addon called RazorGenerator - make sure to restart Visual Studio for your addon to be installed.

Tools.png

Create a new file in your project called CSVGenerator.chtml - your file could be called anything and your project can be any type, but the file has to be .cshtml

Go to the Properties view of the file you just created and set RazorGenerator in the Custom Tool input box.

RazorGen.png

Now as you'll probably notice, the custom tool will run and create a code file from the Razor template you've created. In this file you'll get the auto generated code to create your CSV.

To get started, let's also make a class that contains some business entity, so we can use that as our data to generate the CSV from:

With our class in place, let's start with our Razor Template - open up your file and put this in:

So what we're telling the template engine is that we want our generated file to be a regular template file, and we want a public member in the generated class called model, which we can set to whatever we want. This is essentially exactly what you'll see happen in MVC, but just behind the scenes. In this case, we can give our template multiple parameters and just set those in our code.

Our template is done, and now is only the business of executing it and generating the final form of our beautiful CSV:

As you input that code, you'll find that TransformText is not found in the CSVGenerator implementation, and to achieve that you need to install a NuGet package called RazorGenerator.Templating

Note that the member in our CSVGenerator class called "model" is the one we defined earlier and that could literally be anything. We're giving it a list of BusinessClasses, but you could have a tree or hierarchy of absolutely anything or multiple different parameters if you liked.

In addition to this, we are using Trim at the end of our call on the output from our generated template, to remove any leading line feeds caused by the initialization code in our template.

And here's our output:

4,Joel,0:00 3,Pete,0:00 1,Susannah,0:00 2,Max,0:00

Download sample code

There you have it - a simple, fairly elegant way of achieving CSV files or other template based files such as html or pdf files.

For pdf files, I've used a method similar to this - if you'd like for me to make a post about that, leave a comment.

Cheers