1
1
# CsvExport
2
- A very simple CSV-export tool for C#, code ispired by a thread at Stackoverflow, (C) Chris Hulbert
3
-
4
- This was previously published as a "Gist" but I moved it here, for easier forking/contributing.
2
+ A very simple and fast CSV-export tool for C#.
5
3
6
4
[ ![ .NET] ( https://github.com/jitbit/CsvExport/actions/workflows/dotnet.yml/badge.svg )] ( https://github.com/jitbit/CsvExport/actions/workflows/dotnet.yml )
7
5
@@ -12,17 +10,24 @@ This was previously published as a "Gist" but I moved it here, for easier forkin
12
10
3 . Exports dates in timezone-proof format
13
11
4 . Extremely easy to use
14
12
5 . NET Standard 2.0 library (compatible with both .NET Core and .NET Framework)
13
+ 6 . 30 times faster than CsvHelper
14
+ 7 . 4-times less memory usage
15
15
16
16
## Benchmarks
17
17
18
- | Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
19
- | ---------- | ------------:| ----------:| ---------:| --------:| -------:| ----------:|
20
- | CsvHelper | 1,311.23 us | 16.935 us | 0.928 us | 15.6250 | 7.8125 | 107.42 KB |
21
- | CsvExport (this library) | 32.24 us | 1.365 us | 0.075 us | 4.7607 | 0.2441 | 29.37 KB |
18
+ | Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
19
+ | ------------------ | ------------:| ----------:| ---------:| --------:| -------:| ----------:|
20
+ | CsvHelper | 1,300.38 us | 32.043 us | 1.756 us | 17.5781 | 7.8125 | 114.25 KB |
21
+ | CsvExport_Manual | 31.22 us | 5.750 us | 0.315 us | 4.7607 | 0.2441 | 29.37 KB |
22
+ | CsvExport_Typed | 52.68 us | 1.453 us | 0.080 us | 4.7607 | 0.1221 | 29.46 KB |
23
+
24
+ This benchmark is generating a 100-line CSV file with 4 columns. Check the "SpeedBenchmarks" code.
22
25
23
26
## Usage example:
24
27
25
- Install via Nuget ` Install-Package CsvExport ` then:
28
+ Install via Nuget ` Install-Package CsvExport `
29
+
30
+ For "manual" CSV generation use this:
26
31
27
32
``` c#
28
33
var myExport = new CsvExport ();
@@ -41,6 +46,35 @@ myExport["Date Opened"] = new DateTime(2005, 1, 1, 9, 30, 0);
41
46
return File (myExport .ExportToBytes (), " text/csv" , " results.csv" );
42
47
```
43
48
49
+ For generating CSV out of a typed ` List<T> ` of objects:
50
+
51
+ ``` c#
52
+
53
+ public class Foo
54
+ {
55
+ public string Region { get ; set ; }
56
+ public int Sales { get ; set ; }
57
+ public DateTime DateOpened { get ; set ; }
58
+ }
59
+
60
+ var list = new List <Foo >
61
+ {
62
+ new Foo { Region = " Los Angeles" , Sales = 123321 , DateOpened = DateTime .Now },
63
+ new Foo { Region = " Canberra in Australia" , Sales = 123321 , DateOpened = DateTime .Now },
64
+ };
65
+
66
+ var myExport = new CsvExport ();
67
+ myExport .AddRows (list );
68
+ string csv = myExport .Export ();
69
+ ```
70
+ Configuring is done via constructor parameters:
71
+
72
+ ```
73
+ var myExport = new CsvExport(columnSeparator: ",", includeColumnSeparatorDefinitionPreamble: true, includeHeaderRow: true);
74
+ ```
75
+
76
+ Also, methods ` ExportToFile ` and ` ExportToBytes ` offer an optional encoding parameter.
77
+
44
78
### License
45
79
46
80
The code is licensed under * MIT License* .
0 commit comments