EasyCsv.Core 1.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyCsv.Core --version 1.0.7
NuGet\Install-Package EasyCsv.Core -Version 1.0.7
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasyCsv.Core" Version="1.0.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyCsv.Core --version 1.0.7
#r "nuget: EasyCsv.Core, 1.0.7"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EasyCsv.Core as a Cake Addin
#addin nuget:?package=EasyCsv.Core&version=1.0.7

// Install EasyCsv.Core as a Cake Tool
#tool nuget:?package=EasyCsv.Core&version=1.0.7

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
  • Support for larger number of types: byte[], string, Stream, TextReader, Objects<T>, IBrowserFile, IFormFile
  • 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

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. To recalculate ContentBytes and ContentStr, call easyCsv.CalculateContent(). 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 a set of methods for manipulating CSV data. Some examples include:

No matter what you manipulate, don't forget to call easyCsv.CalculateContent() after you are done doing your manipulations. easyCsv.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 recalculated when easyCsv.CalculateContent() is called.

Remove a column:

    var easyCsv = await EasyCsvFactory.FromStreamAsync(fileStream)
    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 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 () { "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);

Map values in a column:

    // before "header1,header2\nOldValue,OldValue";
    var valueMapping = new Dictionary<object, object>
    {
        { "OldValue", "NewValue" }
    };
    easyCsv.MapValuesInColumn("header1", valueMapping);
    // after "header1,header2\nNewValue,OldValue";

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

    // before  header1,header2
    //          value1,value2"
    var newRow2Columns = new Dictionary<string, object>()
    {
        {"header1", "other 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 herein header3 column")
           .InsertRow(newRow3Columns)
           .FilterRows(row => (string)row["header1"] != "other header1 value")
           .CalculateFileContent();
           
    // after header1,header2,header3
             value1,value2,value given to all rows here in header3 column
             header1 value, header2 value, header3 value
            // Not the data in newRow2Columns because it got filtered

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 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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on EasyCsv.Core:

Package Downloads
EasyCsv

A tool that will make working with CSVs feel like a breeze. Built on top of CSVHelper

EasyCsv.Files

Extends support for IBrowserFiles and IFormFiles

EasyCsv.Processing

A package built ontop of CSV Helper to make working with CSVs even easier

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0-beta1 78 4/29/2024
1.0.29 123 3/6/2024
1.0.28 129 2/1/2024
1.0.27 117 1/26/2024
1.0.26 194 12/21/2023
1.0.25 179 12/3/2023
1.0.24 107 12/3/2023
1.0.23 183 8/10/2023
1.0.22 159 8/7/2023
1.0.21 240 4/26/2023
1.0.16 220 4/26/2023
1.0.15 213 4/26/2023
1.0.14 275 4/7/2023
1.0.13 263 4/7/2023
1.0.12 255 4/7/2023
1.0.11 278 3/31/2023
1.0.10 289 3/31/2023
1.0.9 293 3/31/2023
1.0.8 222 3/30/2023
1.0.7 213 3/29/2023
1.0.6 223 3/29/2023
1.0.5 230 3/29/2023
1.0.4 233 3/29/2023
1.0.3 240 3/29/2023
1.0.2 249 3/27/2023
1.0.1 227 3/27/2023
1.0.0 328 3/27/2023