EasyCsv.Files
1.0.5
See the version list below for details.
dotnet add package EasyCsv.Files --version 1.0.5
NuGet\Install-Package EasyCsv.Files -Version 1.0.5
<PackageReference Include="EasyCsv.Files" Version="1.0.5" />
paket add EasyCsv.Files --version 1.0.5
#r "nuget: EasyCsv.Files, 1.0.5"
// Install EasyCsv.Files as a Cake Addin #addin nuget:?package=EasyCsv.Files&version=1.0.5 // Install EasyCsv.Files as a Cake Tool #tool nuget:?package=EasyCsv.Files&version=1.0.5
EasyCsv Nuget
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
- Fluent API for method chaining
- 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
Installation
Install the EasyCsv package via NuGet:
NuGet\Install-Package EasyCsv
Usage
Add EasyCsv services to your .NET project
In your Startup.cs
or in the class where you configure your services, add the following code to register EasyCsv services:
public void ConfigureServices(IServiceCollection services)
{
services.AddEasyCsvFactories();
// services.AddEasyCsvFactory();
// services.AddEasyFileCsvFactory();
}
Read a CSV file
The is automatically done when you use the IEasyCsvFactory
or IEasyCsvFileFactory
to create your ICsvService
. To recalculate ContentBytes
and ContentStr
, call easyCsv.CalculateContent()
[Inject] private IEasyCsvFileFactory EasyCsvFileFactory { get; set; }
[Inject] private IEasyCsvFactory EasyCsvFactory { get; set; }
=============================================================
IBrowserFile file = files[0];
var strCsv = "header1\nheader1value";
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 a set of methods for manipulating CSV data. Some examples include:
No matter what you manipulate, don't forget to call csvService.CalculateContent()
after you are done doing your manipulations. csvService.CsvContent
is the only content that is always up to date. ContentStr
and ContentBytes
are calculated when you create an IEasyCsv
, after that they are only calculated when csvService.CalculateContent()
is called.
Remove a column:
var easyCsv = EasyCsvFactory.CreateFromBytes(fileContent)
easyCsv.RemoveColumn("header2");
Add column with default value
easyCsv.AddColumn("column name", "value given to all rows in column/header field", upsert: true);
Replace header row
You can replice all the headers in the header row of this CSV. The count of new header row must match count of the current CsvContent
List<string> newHeaderRow = new () { "header1", "header2", "header3" }
easyCsv.ReplaceHeaderRow(newHeaderRow);
Filter rows:
This would remove any row where the value of header1 is less than 10. Would throw an error if any value couldn't be converted to an int.
easyCsv.FilterRows(row => (int)row["header1"] > 10);
Replace values in a column:
var valueMapping = new Dictionary<object, object>
{
{ "OldValue", "NewValue" }
};
easyCsv.ReplaceValuesInColumn("header1", valueMapping);
Sort data by column:
The easiest way is to sort by a column to give them in alphabetical order based on that column, but you can also provide a Func<IDictionary<string, object>, TKey>. to sort like this csvService.SortCsv(row => row["FieldName"].ToString().Length, ascending: false);
. This would sort rows by the lengths of fields in column "FieldName"
easyCsv.SortCsv("header1", ascending: true);
Read directly to objects
Do whatever operations you need to the csv, then read it directly into objects
List<Person> people = easyCsv.GetRecordsAsync<Person>();
Chain Method Calls Fluently
var newRow2Columns = new Dictionary<string, object>()
{
{"header1", "not header1 value"},
{"header2", "header2 value"}
};
var newRow3Columns = new Dictionary<string, object>()
{
{"header1", "header1 value"},
{"header2", "header2 value"},
{"header3", "header3 value"}
};
easyCsv.InsertRow(newRow)
.AddColumn("header3", "value given to all rows in header3 column")
.InsertRow(newRow3Columns)
.FilterRows(row => (string)row["header1"] != "header1 value") // Would remove newRow2Columns (Which would have 3 columns rights here because of AddColumn)
.CalculateFileContent();
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.
License EasyCsv is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net6.0
- EasyCsv.Core (>= 1.0.5)
- Microsoft.AspNetCore.Components.Web (>= 6.0.12)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
net7.0
- EasyCsv.Core (>= 1.0.5)
- Microsoft.AspNetCore.Components.Web (>= 6.0.12)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on EasyCsv.Files:
Package | Downloads |
---|---|
EasyCsv
A tool that will make working with CSVs feel like a breeze. Built on top of CSVHelper |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.1 | 132 | 10/2/2024 |
2.0.0-beta8.2 | 65 | 6/4/2024 |
2.0.0-beta8.1 | 53 | 5/30/2024 |
2.0.0-beta8 | 111 | 5/30/2024 |
2.0.0-beta7 | 120 | 5/25/2024 |
2.0.0-beta6.3 | 59 | 5/20/2024 |
2.0.0-beta6.2 | 58 | 5/16/2024 |
2.0.0-beta6 | 117 | 5/12/2024 |
2.0.0-beta5 | 106 | 5/3/2024 |
2.0.0-beta4 | 76 | 5/3/2024 |
2.0.0-beta3 | 94 | 5/2/2024 |
1.0.23 | 640 | 8/10/2023 |
1.0.22 | 168 | 8/7/2023 |
1.0.21 | 207 | 4/26/2023 |
1.0.16 | 195 | 4/26/2023 |
1.0.15 | 176 | 4/26/2023 |
1.0.14 | 269 | 4/7/2023 |
1.0.13 | 229 | 4/7/2023 |
1.0.12 | 214 | 4/7/2023 |
1.0.11 | 244 | 3/31/2023 |
1.0.10 | 245 | 3/31/2023 |
1.0.9 | 246 | 3/31/2023 |
1.0.8 | 218 | 3/30/2023 |
1.0.7 | 208 | 3/29/2023 |
1.0.6 | 204 | 3/29/2023 |
1.0.5 | 220 | 3/29/2023 |
1.0.4 | 209 | 3/29/2023 |
1.0.3 | 212 | 3/29/2023 |
1.0.2 | 245 | 3/27/2023 |
1.0.1 | 222 | 3/27/2023 |
1.0.0 | 301 | 3/27/2023 |