Skip to content

Repository files navigation

CsvExport

A very simple and very fast CSV-export tool for C#.

.NET

Focused on speed and memory usage when streaming large exports in web-apps.

V3 Breaking changes:

  • .NET 8 targeting (use v2 for .NET Framework, we'll backport critical fixes)
  • Uses char instead of string for column separator

Features

  1. 33 times faster than CsvHelper
  2. 3X less memory usage
  3. Streaming support (CSV writer does not buffer large CSVs in memory)
  4. Excel-compatible export (separator detected automatically, friendly-trimming rows and values for compatibility)
  5. Escapes commas, quotes, multiline text
  6. Exports dates in timezone-proof format
  7. Extremely easy to use
  8. 4-times less memory usage

Benchmarks

MethodMeanErrorStdDevGen0Gen1Allocated
😟 CsvHelper372.90 us390.842 us21.423 us9.76564.882885.4 KB
✅ CsvExport_Manual12.71 us1.040 us0.057 us3.58580.198429.35 KB
✅ CsvExport_GenericType13.22 us1.240 us0.068 us3.58580.228929.39 KB

This benchmark is generating a 100-line CSV file with 4 columns. Check the "SpeedBenchmarks" code.

Usage examples:

Install via Nuget Install-Package CsvExport

For "manual" CSV ad-hoc generation use this:

varmyExport=newCsvExport();myExport.AddRow();myExport["Region"]="Los Angeles, USA";myExport["Sales"]=100000;myExport["Date Opened"]=newDateTime(2003,12,31);myExport.AddRow();myExport["Region"]="Canberra \"in\" Australia";myExport["Sales"]=50000;myExport["Date Opened"]=newDateTime(2005,1,1,9,30,0);//save as filemyExport.ExportToFile("results.csv");

For generating CSV out of a typed List<T> of objects:

publicclassFoo{publicstringRegion{get;set;}publicintSales{get;set;}publicDateTimeDateOpened{get;set;}}varlist=newList<Foo>{newFoo{Region="Los Angeles",Sales=123321,DateOpened=DateTime.Now},newFoo{Region="Canberra in Australia",Sales=123321,DateOpened=DateTime.Now},};varmyExport=newCsvExport();myExport.AddRows(list);stringcsv=myExport.Export();

Configuring is done via constructor parameters:

varmyExport=newCsvExport(columnSeparator:',',includeColumnSeparatorDefinitionPreamble:true,//Excel wants this in CSV filesincludeHeaderRow:true);

Also, methods ExportToFile and WriteToStream and ExportToBytes offer an optional encoding parameter.

Using with ASP.NET Core:

For big CSV files (megabytes) use WriteToStreamAsync and write to Response.Body directly. This is very important to save memory usage. Here's a handy helper class:

publicclassCsvExportResult(Csv.CsvExportcsv,stringfileName):ActionResult{publicoverrideTaskExecuteResultAsync(ActionContextctx){varres=ctx.HttpContext.Response;res.ContentType="text/csv";res.Headers.ContentDisposition=$"attachment; filename=\"{fileName}\"";returncsv.WriteToStreamAsync(res.Body,cancellationToken:ctx.HttpContext.RequestAborted);}}//usage in MVC actionreturnnewCsvExportResult(csvExport,"filename.csv");

License

The code is licensed under MIT License.

Sucessfully tested for years in production with our Jitbit Helpdesk Ticketing System

About

Very simple CSV-export tool for C#

Topics

Resources

Stars

177 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages