Monday, March 26, 2018

Exporting CSVs with ASP.NET and C#

Exporting CSV is a requirement developers often get. On this post, let's review how to do it using ASP.NET and C#
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. Please don't. There are important considerations around performance, security, storage and details of the CSV format itself that most likely your code will not comply with.

So why not just use available free code like the very good CsvHelper Nuget Package. I've been using that package for a few years and never had problems with it.

Let's do Code then!

Enough of talk, let's do some code. 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. Notes:
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;

Don't forget to Refactor

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.

Remember: 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.

Final Thoughts

This example covers a simple scenario of loading, building and exporting the data to the users. It should work well for up to 5mb. 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

Why you should start using .NET CoreBuilding and Running ASP.NET Core apps on Linux
.NET Core and .NET merging as .NET 5.0
Copying data between Azure databases in 3 easy steps
Simplifying Razor logic with C# Local Functions in ASP.NET Core
Why you should start using .NET Core
Package Management in .NET Core
Building and Running ASP.NET Core apps on Linux
A simple chat room in Vue.Js

For more .NET Core posts on this blog, please click here.