ObjectToCsv 2.0.0
dotnet add package ObjectToCsv --version 2.0.0
NuGet\Install-Package ObjectToCsv -Version 2.0.0
<PackageReference Include="ObjectToCsv" Version="2.0.0" />
<PackageVersion Include="ObjectToCsv" Version="2.0.0" />
<PackageReference Include="ObjectToCsv" />
paket add ObjectToCsv --version 2.0.0
#r "nuget: ObjectToCsv, 2.0.0"
#:package ObjectToCsv@2.0.0
#addin nuget:?package=ObjectToCsv&version=2.0.0
#tool nuget:?package=ObjectToCsv&version=2.0.0
ObjectToCsv
C# Library: Object List to CSV Converter
Overview
This library provides an easy way to convert a list of objects into a CSV (Comma-Separated Values) file. It's particularly useful when you need to export data from your application to a format that can be easily imported into spreadsheet software or other tools.
Features
- Converts a list of objects (such as a collection of custom classes) into a CSV file.
- Handles escaping special characters (like commas and double quotes) within the data.
- Supports custom delimiters (e.g., semicolon, tab) if needed.
Installation
Using NuGet Package Manager:
- Open your project in Visual Studio.
- Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
- Search for "ObjectToCsv" and install it.
- ObjectToCsv is a popular library that simplifies reading and writing CSV files in C#.
Manual Installation:
- Download the ObjectToCsv library from NuGet.
- Add the reference to your project.
Custom Attributes
- In C#, when working with models, it’s common to encounter scenarios where the property names in the model differ from the column names in a CSV file. To address this issue, we can use the
Header
attribute. This attribute facilitates the mapping between CSV columns and model properties. - Additionally, there’s another attribute called
Ordinal
. The purpose of this attribute is to help the library identify the order of CSV headers, which can be useful during data processing.
Usage Example
Suppose you have a class called Person
with properties like Id
, Name
, Age
, and Email
. You want to convert a list of Person
objects to a CSV file.
- Create a list of
Person
objects: - Add
Header
andOrdinal
attribute for properties.
public class PersonObject
{
[Header("Id")]
[Ordinal(0)]
public Guid Id { get; set; }
[Header("Email")]
[Ordinal(1)]
public string Email { get; set; }
[Header("User Name")]
[Ordinal(2)]
public string Name { get; set; }
[Header("Age")]
[Ordinal(3)]
public string Age { get; set; }
[Header("Email")]
[Ordinal(4)]
public string Email { get; set; }
}
using ObjectToCsv;
// Retrieve a list of PersonObject instances to be converted to CSV.
List<PersonObject> users = GetTestObjectList();
// Define a custom date format for DateTime fields in the CSV output.
string dateFormat = "dddd, dd MMMM yyyy"; // Example: "Monday, 01 January 2024"
// Use the CsvUtil library to convert the list of PersonObjects into a CSV-formatted string.
// The dateFormat parameter is optional; if not provided, the default format "dd MMMM yyyy" will be used.
var csvString = CsvUtil.BindCsv<PersonObject>(users, dateFormat);
// csvString now contains the CSV representation of the users list.
Contributing
Feel free to contribute to this library by submitting pull requests or reporting issues on the GitHub repository.
License
This library is released under the MIT License. See the LICENSE file for details.
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 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.