{"id":27062566,"url":"https://github.com/biegehydra/easycsv-dotnet","last_synced_at":"2025-04-05T15:17:49.773Z","repository":{"id":149512289,"uuid":"619184447","full_name":"biegehydra/EasyCsv-Dotnet","owner":"biegehydra","description":"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.","archived":false,"fork":false,"pushed_at":"2024-05-20T21:07:22.000Z","size":613,"stargazers_count":24,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-05-21T21:39:38.207Z","etag":null,"topics":["csharp","csv","csv-export","csv-files","csv-format","csv-parsing","csv-reading","dotnet"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/biegehydra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-26T14:18:52.000Z","updated_at":"2024-05-30T02:48:04.658Z","dependencies_parsed_at":"2024-05-30T02:58:53.414Z","dependency_job_id":null,"html_url":"https://github.com/biegehydra/EasyCsv-Dotnet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biegehydra%2FEasyCsv-Dotnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biegehydra%2FEasyCsv-Dotnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biegehydra%2FEasyCsv-Dotnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biegehydra%2FEasyCsv-Dotnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/biegehydra","download_url":"https://codeload.github.com/biegehydra/EasyCsv-Dotnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353719,"owners_count":20925329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","csv","csv-export","csv-files","csv-format","csv-parsing","csv-reading","dotnet"],"created_at":"2025-04-05T15:17:49.111Z","updated_at":"2025-04-05T15:17:49.761Z","avatar_url":"https://github.com/biegehydra.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NuGet](https://img.shields.io/nuget/dt/EasyCsv?label=NuGet%20Downloads\u0026style=plastic)](https://www.nuget.org/packages/EasyCsv/)\n# EasyCsv\n\nEasyCsv 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.\n\n## Features\n\n* Read and write CSV files\n* Support for larger number of types: byte[], string, Stream, TextReader, Objects\u003cT\u003e, IBrowserFile, IFormFile\n* Fluent API for method chaining\n* Mutations context to limit error proneability\n* Perform basic operations on CSV files, such as adding or removing columns, filtering rows, sorting data, and replacing values in a column\n* Support for dependency injection\n\n## Blazor Components\n\n[Documentation](components.md)\n\n[Example Website](https://d143idkvxttaq3.cloudfront.net/processing)\n\n## Installation\n\nInstall the EasyCsv package via NuGet:\n\n`NuGet\\Install-Package EasyCsv -Version 2.0.0-beta8.2`\n\n## Usage\n\n## Creating an IEasyCsv and reading a CSV file\n\nThe 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.\n\n```csharp\nIBrowserFile file = files[0];\nvar strCsv = \"header1,header2\\nheader1value,header2value\";\nvar easyCsv = await EasyCsvFileFactory.FromIBrowserFileAsync(file);\nvar easyCsv2 = await EasyCsvFactory.FromStringAsync();\n// You can access the ContentStr, ContentBytes, and create C# Objects using GetRecords\u003cT\u003e at this point\n```\n## Manipulate CSV data\n\nEasyCsv provides an assortment of methods for manipulating CSV data. All calls that manipulate the CSV are done through `easyCsv.Manipulate(Action\u003cCSVMuationScope\u003e scope)` or `easyCsv.ManipulateAsync(Action\u003cCSVMuationScope\u003e scope)`. The scope will ensure that the `ContentStr` and `ContentBytes` are up to date after you do manipulations. \n\n## Add/Insert column with default value\n```csharp\neasyCsv.Mutate(mutation =\u003e\n{\n    mutation.AddColumn(\"column name\", \"value given to all rows in column/header field\", upsert: true);\n    mutation.InsertColumn(index: 2, \"different col name\", \"val\");\n});\n```\n## Remove a column:\n```csharp\nvar easyCsv = await EasyCsvFactory.FromStreamAsync(fileStream);\neasyCsv.Mutate(mutation =\u003e mutation.RemoveColumn(\"header2\"));\n```\n## Replace Columns\nRemoves the column of the old header field and upserts all it's values to all the rows of the new header field. CSV\n```csharp\neasyCsv.Mutate(mutation =\u003e mutation.ReplaceColumn(string oldHeaderField, string newHeaderField));\n```\n## Swap Columns\nSwaps the position of columns in a csv. The values follow the columns through swap.\n```csharp\n// Csv Original Headers: \"col1,col2,col3,col4\"\neasyCsv.Mutate(mutation =\u003e\n{\n    mutation.SwapColumns(\"col1\", \"col4\"); // By column name\n    // Headers are now: \"col4,col2,col3,col1\"\n\n    mutation.SwapColumns(1, 2); // By column index\n    // Headers are now: \"col4,col3,col2,col1\"\n});\n```\n\n## Move Column\nMoves a column to a new index. The values follow the column through swap. All other columns are shifted right or left accordingly.\n```csharp\n// Csv Original Headers: \"col1,col2,col3,col4\"\neasyCsv.Mutate(mutation =\u003e\n{\n    mutation.MoveColumn(\"col4\", 0); // Moves \"col4\" to index 0\n    // Headers are now: \"col4,col1,col2,col3\"\n\n    mutation.MoveColumn(3, 0); // Moves the column at index 3 (col3) to index 0\n    // Headers are now: \"col3,col4,col1,col2\"\n});\n```\n\n## Replace header row\nYou 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***\n```csharp\nList\u003cstring\u003e newHeaderRow = new () { \"newHeader1\", \"newHeader2\", \"newHeader3\" }\neasyCsv.Mutate(mutation =\u003e mutation.ReplaceHeaderRow(newHeaderRow));\n```\n## Remove unused data\nRemoves any header that does match a public property on the type param T.\n```csharp\n// Removes all fields that don't match public property on Person\nawait easyCsv.MutateAsync(mutation =\u003e await mutation.RemoveUnusedHeadersAsync\u003cPerson\u003e(caseInsensitive:true));\n```\n## Filter rows:\nThis 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.\n```csharp\neasyCsv.Mutate(mutation =\u003e mutation.FilterRows(row =\u003e (int)row[\"header1\"] \u003e 10));\n```\n## Map values in a column:\n```csharp\n// before \"header1,header2\\nOldValue,OldValue\";\nvar valueMapping = new Dictionary\u003cobject, object\u003e\n{\n    { \"OldValue\", \"NewValue\" }\n};\neasyCsv.Mutate(mutation =\u003e mutation.MapValuesInColumn(\"header1\", valueMapping));\n// after \"header1,header2\\nNewValue,OldValue\";\n```\n## Sort data by column:\nYou can provide a Func\u003cIDictionary\u003cstring, object\u003e, TKey\u003e. to sort like this `easyCsv.SortCsv(row =\u003e row[\"FieldName\"].ToString().Length, ascending: false);`. This would sort rows by the lengths of fields in column \"header1\"\n```csharp\neasyCsv.Mutate(mutation =\u003e mutation.SortCsv(\"header1\", ascending: true));\n```\n## Combine CSVs\nSome 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.\n```csharp\nvar easyCsv1 = EasyCsvFactory.FromObjects\u003cPerson\u003e(people1);\nvar easyCsv2 = EasyCsvFactory.FromObjects\u003cPerson\u003e(people2);\nvar combinedCsv = easyCsv1.Combine(easyCsv2); \n// The configurations from easyCsv1 will used in the combinedCsv\n// This can also be done in the mutation context\n```\n## Read directly to objects\nDo whatever operations you need to the csv, then read it directly into objects\n```csharp\nList\u003cPerson\u003e people = easyCsv.GetRecordsAsync\u003cPerson\u003e();\n```\n## Crud Operations\n\nI also have some CRUD operations for directly working with rows. UpdateRow, UpsertRow, DeleteRow, AddRow, etc\n\n## Convenience methods\n\nI include plenty of convenience methods on EasyCsv such as `Clone()`, `ColumnNames()`, `GetRowCount()`, `ContainsHeader()`, `Clear()`\n\n## Chain Method Calls Fluently\n\n```csharp\n// before  header1,header2, header3\n//          value1,value2, value3\n//          value1,value2, value3\neasyCsv.Mutate(mutations =\u003e mutations.RemoveColumn(\"header2\")\n                                     .AddColumn(\"header4\", \"value4\")\n                                     .ReplaceColumn(\"header1\", \"newHeader1\"));\n       \n// after  newHeader1, header3, header4\n//          value1, value3, value4\n//          value1, value3, value4\n```\n\n## Operate on individual rows\nThis 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. \n\nIn the example, the Id column is the first column in the csv. This inserts a name column after the Id column.\n\n**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.\n**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. \n```csharp\nDictionary\u003clong, string\u003e idToNameDict = GetCustomerNames();\n// Before \"Id,Company,Position\"\nawait csv.MutateAsync(x =\u003e\n{\n    x.InsertColumn(1, \"Name\");\n    foreach (var row in x.CsvContent)\n    {\n        if (row[\"Id\"] is string str \u0026\u0026 long.TryParse(str, out var num) \u0026\u0026 idToNameDict.TryGetValue(num, out string name))\n        {\n            row[\"Name\"] = name;\n        }\n        else\n        {\n            row[\"Name\"] = \"Unknown\";\n        }\n    }\n});\n// After \"Id,Name,Company,Position\"\n```\n\nFor more methods and usage examples, please refer to the EasyCsv documentation and source code.\n\n## Contributing\nI 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.\n\n## Acknowledgements\n\nThis library makes use of the following third-party dependencies:\n\n### CsvHelper\n\nEasyCsv uses [CsvHelper](https://joshclose.github.io/CsvHelper/) to read and write CSV files. CsvHelper is licensed under the [Microsoft Public License (Ms-PL)](https://opensource.org/licenses/MS-PL). We would like to thank the authors and contributors of CsvHelper for their work on this excellent library.\n\n## License Information\n\nPlease note the following licensing terms for EasyCsv's packages:\n\n- **General License**: All packages are licensed under the MIT License, except for the code located in the `EasyCsv.Components` folder.\n- **Special Licensing for EasyCsv.Components**: The code within the `EasyCsv.Components` folder is licensed under the AGPL-3.0 License.\n\n### Usage Guidelines:\n\n1. **Internal Use**: If the `EasyCsv.Components` package is solely used within your company by company employees for internal operations, **and not exposed to external customers**, you may utilize these components under the MIT License. This allows your team to integrate and use these components internally without the broader requirements of the AGPL-3.0 License. Please still include the AGPL-3.0 License in your project if you use a clone.\n\n2. **External Use**: Any deployment of the `EasyCsv.Components` in a customer facing application or service, for-profit or not, is strictly governed by the AGPL-3.0 License. This includes any form of service provided over a network where the components are used to interact with users. In such scenarios, you must comply with all provisions of the AGPL-3.0 License, including but not limited to making the source code available to all users.\n\n\n## Known Issues and Limitations\n\nLicense\nEasyCsv is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiegehydra%2Feasycsv-dotnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbiegehydra%2Feasycsv-dotnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiegehydra%2Feasycsv-dotnet/lists"}