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 an ASP.NET web application using C#.

Exporting CSVs from a the database is a very common requirement. Since building CSV files seems simple, it's common to see developers building their own. However, before thinking about writing your own parser, remember that there are important considerations including performance, security, and details of the CSV format itself that most likely your code will not comply with. With all that said, it's best practice to use an existing tool which, for us .NET developers is the CsvHelper Nuget Package.

Overall, this exercise 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. Fetch the 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 the previous code. The most important bits are on:
  • 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

About the Author

Bruno Hildenbrand      
Principal Architect, HildenCo Solutions.