EasyCsv 2.0.0-beta5
See the version list below for details.
dotnet add package EasyCsv --version 2.0.0-beta5
NuGet\Install-Package EasyCsv -Version 2.0.0-beta5
<PackageReference Include="EasyCsv" Version="2.0.0-beta5" />
paket add EasyCsv --version 2.0.0-beta5
#r "nuget: EasyCsv, 2.0.0-beta5"
// Install EasyCsv as a Cake Addin #addin nuget:?package=EasyCsv&version=2.0.0-beta5&prerelease // Install EasyCsv as a Cake Tool #tool nuget:?package=EasyCsv&version=2.0.0-beta5&prerelease
EasyCsv
EasyCsv is a simple and efficient .NET library for handling CSV files in your projects. With a fluent user-friendly API, it allows you to easily read, write, and manipulate CSV files with a minimal amount of code.
Features
- Read and write CSV files
- Support for larger number of types: byte[], string, Stream, TextReader, Objects<T>, IBrowserFile, IFormFile
- Fluent API for method chaining
- Mutations context to limit error proneability
- Perform basic operations on CSV files, such as adding or removing columns, filtering rows, sorting data, and replacing values in a column
- Support for dependency injection
Blazor Components
Installation
Install the EasyCsv package via NuGet:
NuGet\Install-Package EasyCsv
Usage
Creating an IEasyCsv and reading a CSV file
The reading of the csv is automatically done when you use the EasyCsvFactory
or EasyCsvFileFactory
to create your IEasyCsv
. It is also automatically done when you call easyCsv.Mutate()
or easyCsv.MutateAsync()
unless you set the saveChanges
flag to false. WARNING. All From(Type)
methods will return null
if something goes wrong or the csv contains 0 rows.
IBrowserFile file = files[0];
var strCsv = "header1,header2\nheader1value,header2value";
var easyCsv = await EasyCsvFileFactory.FromIBrowserFileAsync(file);
var easyCsv2 = await EasyCsvFactory.FromStringAsync();
// You can access the ContentStr, ContentBytes, and create C# Objects using GetRecords<T> at this point
Manipulate CSV data
EasyCsv provides an assortment of methods for manipulating CSV data. All calls that manipulate the CSV are done through easyCsv.Manipulate(Action<CSVMuationScope> scope)
or easyCsv.ManipulateAsync(Action<CSVMuationScope> scope)
. The scope will ensure that the ContentStr
and ContentBytes
are up to date after you do manipulations.
Add/Insert column with default value
easyCsv.Mutate(mutation =>
{
mutation.AddColumn("column name", "value given to all rows in column/header field", upsert: true);
mutation.InsertColumn(index: 2, "different col name", "val");
});
Remove a column:
var easyCsv = await EasyCsvFactory.FromStreamAsync(fileStream);
easyCsv.Mutate(mutation => mutation.RemoveColumn("header2"));
Replace Columns
Removes the column of the old header field and upserts all it's values to all the rows of the new header field. CSV
easyCsv.Mutate(mutation => mutation.ReplaceColumn(string oldHeaderField, string newHeaderField));
Swap Columns
Swaps the position of columns in a csv. The values follow the columns through swap.
// Csv Original Headers: "col1,col2,col3,col4"
easyCsv.Mutate(mutation =>
{
mutation.SwapColumns("col1", "col4"); // By column name
// Headers are now: "col4,col2,col3,col1"
mutation.SwapColumns(1, 2); // By column index
// Headers are now: "col4,col3,col2,col1"
});
Move Column
Moves a column to a new index. The values follow the column through swap. All other columns are shifted right or left accordingly.
// Csv Original Headers: "col1,col2,col3,col4"
easyCsv.Mutate(mutation =>
{
mutation.MoveColumn("col4", 0); // Moves "col4" to index 0
// Headers are now: "col4,col1,col2,col3"
mutation.MoveColumn(3, 0); // Moves the column at index 3 (col3) to index 0
// Headers are now: "col3,col4,col1,col2"
});
Replace header row
You can replace all the headers in the header row of this CSV. The number of headers in the new row must match the number of headers current CsvContent or no operation will be performed
List<string> newHeaderRow = new () { "newHeader1", "newHeader2", "newHeader3" }
easyCsv.Mutate(mutation => mutation.ReplaceHeaderRow(newHeaderRow));
Remove unused data
Removes any header that does match a public property on the type param T.
// Removes all fields that don't match public property on Person
await easyCsv.MutateAsync(mutation => await mutation.RemoveUnusedHeadersAsync<Person>(caseInsensitive:true));
Filter rows:
This would remove any row where the value of header1 column is less than 10. Would throw an error if any value couldn't be converted to an int.
easyCsv.Mutate(mutation => mutation.FilterRows(row => (int)row["header1"] > 10));
Map values in a column:
// before "header1,header2\nOldValue,OldValue";
var valueMapping = new Dictionary<object, object>
{
{ "OldValue", "NewValue" }
};
easyCsv.Mutate(mutation => mutation.MapValuesInColumn("header1", valueMapping));
// after "header1,header2\nNewValue,OldValue";
Sort data by column:
You can provide a Func<IDictionary<string, object>, TKey>. to sort like this easyCsv.SortCsv(row => row["FieldName"].ToString().Length, ascending: false);
. This would sort rows by the lengths of fields in column "header1"
easyCsv.Mutate(mutation => mutation.SortCsv("header1", ascending: true));
Combine CSVs
Some care will need to be taken with this since the headers must match exactly, however it is perfect to use when you know you have two csvs that were read an object of type T.
var easyCsv1 = EasyCsvFactory.FromObjects<Person>(people1);
var easyCsv2 = EasyCsvFactory.FromObjects<Person>(people2);
var combinedCsv = easyCsv1.Combine(easyCsv2);
// The configurations from easyCsv1 will used in the combinedCsv
// This can also be done in the mutation context
Read directly to objects
Do whatever operations you need to the csv, then read it directly into objects
List<Person> people = easyCsv.GetRecordsAsync<Person>();
Crud Operations
I also have some CRUD operations for directly working with rows. UpdateRow, UpsertRow, DeleteRow, AddRow, etc
Convenience methods
I include plenty of convenience methods on EasyCsv such as Clone()
, ColumnNames()
, GetRowCount()
, ContainsHeader()
, Clear()
Chain Method Calls Fluently
// before header1,header2, header3
// value1,value2, value3
// value1,value2, value3
easyCsv.Mutate(mutations => mutations.RemoveColumn("header2")
.AddColumn("header4", "value4")
.ReplaceColumn("header1", "newHeader1"));
// after newHeader1, header3, header4
// value1, value3, value4
// value1, value3, value4
Operate on individual rows
This code shows how you can add a "Name" column to a csv and populate it with values based on the Id column in the csv.
In the example, the Id column is the first column in the csv. This inserts a name column after the Id column.
Note: When operating on individual rows, it is your job to ensure that column structure is maintained in each row. Without the else statement, there would be a chance that some rows are missing a column called "Name" leading to undefined behaviour. Something you can do is call InsertColumn
or AddColumn
before adding an optional value to all rows which will ensure that each row at least has the column.
Note-2: Expect all values to be string. The only times that isn't true is if you add a column with a default value other than a string.
Dictionary<long, string> idToNameDict = GetCustomerNames();
// Before "Id,Company,Position"
await csv.MutateAsync(x =>
{
x.InsertColumn(1, "Name");
foreach (var row in x.CsvContent)
{
if (row["Id"] is string str && long.TryParse(str, out var num) && idToNameDict.TryGetValue(num, out string name))
{
row["Name"] = name;
}
else
{
row["Name"] = "Unknown";
}
}
});
// After "Id,Name,Company,Position"
For more methods and usage examples, please refer to the EasyCsv documentation and source code.
Contributing
I gladly welcome contributions to EasyCsv! If you find a bug or have a feature request, please open an issue on the project's GitHub repository. If you would like to contribute code, please submit a pull request.
Acknowledgements
This library makes use of the following third-party dependencies:
CsvHelper
EasyCsv uses CsvHelper to read and write CSV files. CsvHelper is licensed under the Microsoft Public License (Ms-PL). We would like to thank the authors and contributors of CsvHelper for their work on this excellent library.
Known Issues and Limitations
License EasyCsv is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- EasyCsv.Core (>= 2.0.0-beta5)
- EasyCsv.Files (>= 2.0.0-beta5)
- EasyCsv.Processing (>= 2.0.0-beta5)
-
.NETStandard 2.1
- EasyCsv.Core (>= 2.0.0-beta5)
- EasyCsv.Files (>= 2.0.0-beta5)
- EasyCsv.Processing (>= 2.0.0-beta5)
-
net6.0
- EasyCsv.Core (>= 2.0.0-beta5)
- EasyCsv.Files (>= 2.0.0-beta5)
- EasyCsv.Processing (>= 2.0.0-beta5)
-
net7.0
- EasyCsv.Core (>= 2.0.0-beta5)
- EasyCsv.Files (>= 2.0.0-beta5)
- EasyCsv.Processing (>= 2.0.0-beta5)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on EasyCsv:
Package | Downloads |
---|---|
EasyCsv.Components
A collection of blazor components for reading and mapping csvs. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.1 | 115 | 10/2/2024 |
2.0.0-beta8.2 | 69 | 6/4/2024 |
2.0.0-beta8.1 | 52 | 5/30/2024 |
2.0.0-beta8 | 76 | 5/30/2024 |
2.0.0-beta7 | 93 | 5/25/2024 |
2.0.0-beta6.3 | 62 | 5/20/2024 |
2.0.0-beta6.2 | 60 | 5/16/2024 |
2.0.0-beta6 | 88 | 5/12/2024 |
2.0.0-beta5 | 88 | 5/3/2024 |
2.0.0-beta4 | 45 | 5/3/2024 |
2.0.0-beta3 | 51 | 5/2/2024 |
2.0.0-beta1 | 100 | 4/29/2024 |
1.0.29 | 307 | 3/6/2024 |
1.0.28 | 185 | 2/1/2024 |
1.0.27 | 126 | 1/26/2024 |
1.0.26 | 210 | 12/21/2023 |
1.0.25 | 158 | 12/3/2023 |
1.0.24 | 91 | 12/3/2023 |
1.0.23 | 155 | 8/10/2023 |
1.0.22 | 121 | 8/7/2023 |
1.0.21 | 154 | 4/26/2023 |
1.0.20 | 134 | 4/26/2023 |
1.0.19 | 143 | 4/26/2023 |
1.0.18 | 172 | 4/7/2023 |
1.0.17 | 150 | 4/7/2023 |
1.0.16 | 157 | 4/7/2023 |
1.0.15 | 183 | 3/31/2023 |
1.0.14 | 161 | 3/31/2023 |
1.0.13 | 160 | 3/31/2023 |
1.0.12 | 158 | 3/30/2023 |
1.0.11 | 162 | 3/29/2023 |
1.0.10 | 164 | 3/29/2023 |
1.0.9 | 183 | 3/29/2023 |
1.0.8 | 183 | 3/29/2023 |
1.0.7 | 177 | 3/29/2023 |
1.0.6 | 189 | 3/27/2023 |
1.0.5 | 179 | 3/27/2023 |
1.0.4 | 176 | 3/27/2023 |
1.0.3 | 184 | 3/27/2023 |
1.0.2 | 183 | 3/27/2023 |
1.0.1 | 186 | 3/27/2023 |
1.0.0 | 187 | 3/27/2023 |