Monday, March 26, 2018

How to export CSVs with ASP.NET and C#

Exporting CSV is a very common requirement. Today, let's review how to do it elegantly using ASP.NET and C#.
Photo by Mika Baumeister on Unsplash
On a previous post we discussed how to import CSVs using .NET and C#. Today we continue discussing CSVs, specifically, how to generate and export CSVs directly from the database to the users from via an ASP.NET web application using C#. Exporting data from the database to the user on the CSV format is a very common requirement. Since the CSV format is pretty standard, it's also common to see developers building their own. Before thinking about writing yours remember that there are important considerations around performance, security, storage and details of the CSV format itself that most likely your code will not comply with. A good way to approach this problem is by using the CsvHelper Nuget Package.

The approach here can be broken in 4 steps:
  1. Add a reference to the CsvHelper nuget package in your ASP.NET project (which you saw how on the previous post);
  2. Grab your records form the database
  3. Build your CSV in-memory;
  4. Format and build your response setting an arbitrary file name and returning the request tot the user;

Source Code

All of the above tasks are listed on the code below. Please, read on. We will explain the details below the markup.

Reviewing the Code

Now let's review exactly what happened up there.
  • Lines 12-16: on this example, we used RavenDB as our database. Lines 12-16 describe how to  load the data from the database using an index;
  • Line 15: I'm converting between my db entity (BatchRow) to my view model (CsvLine);
  • Line 26: CSVHelper is doing the magic for us. It's better when it's simple!
  • Line 31: I'm formatting the file name;
  • Line 33: Writing the the response stream;
  • Line 34: closing the response stream;


As I have a constant desire to make my code as clean as possible, I usually review my code before committing and pushing. If there is bloat there, I usually refactor it, making use of my tests to help me refactor with confidence.
If the code is bloated, the performance is below acceptable or if you are not satisfied with the solution, consider refactoring or changing how you architected your solution in the first place. Optimize for correctness first, then for performance.

Final Thoughts

This example covers a simple scenario of loading, building and exporting the data to the users. It should work well for serving up to 5 Mb of data. However, if your required to export a lot of data, you should consider other options such as streaming the data to the users, processing it in the backend (or in a WebJob) and emailing it back to your users. Another option could even be pre-generating the report and just serve it on the click of the download button. Use your creativity!

Also consider that depending on your use cases, this code is yes, too simplistic. But that's the objective. We should never implement what we don't need, yet. Hope it helps!

See Also

For more .NET Core posts on this blog, please click here.
Any comment or suggestion about this post? Please DM me on Twitter at @BrunoHilden