For small to medium datasets where you want full control without external dependencies, you can manually build a CSV string using StringBuilder . Step-by-Step Implementation:
In modern ASP.NET Core MVC or Web API, the most common way to trigger a download is by returning a FileResult . export dataset to csv file download in c#
While manual string building works, it often fails on edge cases like escaped quotes or complex date formatting. The industry standard is CsvHelper , a NuGet package that handles these complexities automatically. For small to medium datasets where you want
For large datasets, storing the entire CSV in memory as a string can cause performance issues or OutOfMemoryExceptions . Using a StreamWriter allows you to write data line-by-line directly to a file or a web response stream. The industry standard is CsvHelper , a NuGet
: Loop through DataTable.Rows and join each field with a comma.
: In web applications, you can write directly to the Response.Body to start the download before the entire file is even generated. 3. Implementing the Download in ASP.NET Core