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 |
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:
- Add a reference to the CsvHelper nuget package in your ASP.NET project (which you saw how on the previous post);
- Fetch the records form the database;
- Build your CSV in-memory;
- 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;
Refactoring
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!
References
See Also
- My journey to 1 million articles read
- How to import CSVs using .NET and C#
- Adding Application Insights telemetry to your ASP.NET Core website
- Creating ASP.NET Core websites with Docker
- Distributed caching in ASP.NET Core using Redis, MongoDB and Docker
- Send emails from ASP.NET Core websites using SendGrid and Azure
- Hosting NuGet packages on GitHub
- Configuration in .NET Core console applications
- A simple chat room in Vue.Js
- Building and Running ASP.NET Core apps on Linux
- Simplifying Razor logic with C# Local Functions in ASP.NET Core
- Why use .NET Core
- An in depth review of the RavenDB Cloud