Skip to content

Commit bb3b214

Browse files
committed
Merge branch 'master' of https://github.com/jitbit/CsvExport
2 parents d6a37de + d60ccf6 commit bb3b214

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ A very simple and very fast CSV-export tool for C#.
1010

1111
## Features
1212

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

2022
## Benchmarks
@@ -46,8 +48,8 @@ myExport["Region"] = "Canberra \"in\" Australia";
4648
myExport["Sales"] = 50000;
4749
myExport["Date Opened"] = new DateTime(2005, 1, 1, 9, 30, 0);
4850

49-
///ASP.NET MVC action example
50-
return File(myExport.ExportAsMemoryStream(), "text/csv", "results.csv");
51+
//save as file
52+
myExport.ExportToFile("results.csv");
5153
```
5254

5355
For generating CSV out of a typed `List<T>` of objects:
@@ -75,13 +77,33 @@ Configuring is done via constructor parameters:
7577

7678
```c#
7779
var myExport = new CsvExport(
78-
columnSeparator: ",",
80+
columnSeparator: ',',
7981
includeColumnSeparatorDefinitionPreamble: true, //Excel wants this in CSV files
8082
includeHeaderRow: true
8183
);
8284
```
8385

84-
Also, methods `ExportToFile` and `ExportAsMemoryStream` and `ExportToBytes` offer an optional encoding parameter.
86+
Also, methods `ExportToFile` and `WriteToStream` and `ExportToBytes` offer an optional encoding parameter.
87+
88+
### Using with ASP.NET Core:
89+
90+
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 heper class:
91+
92+
```c#
93+
public class CsvExportResult(Csv.CsvExport csv, string fileName) : ActionResult
94+
{
95+
public override Task ExecuteResultAsync(ActionContext ctx)
96+
{
97+
var res = ctx.HttpContext.Response;
98+
res.ContentType = "text/csv";
99+
res.Headers.ContentDisposition = $"attachment; filename=\"{fileName}\"";
100+
return csv.WriteToStreamAsync(res.Body, cancellationToken: ctx.HttpContext.RequestAborted);
101+
}
102+
}
103+
104+
//usage in MVC action
105+
return new CsvExportResult(csvExport, "filename.csv");
106+
```
85107

86108
### License
87109

0 commit comments

Comments
 (0)