ObjectToCsv 2.0.0

dotnet add package ObjectToCsv --version 2.0.0
                    
NuGet\Install-Package ObjectToCsv -Version 2.0.0
                    
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="ObjectToCsv" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ObjectToCsv" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ObjectToCsv" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ObjectToCsv --version 2.0.0
                    
#r "nuget: ObjectToCsv, 2.0.0"
                    
#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.
#:package ObjectToCsv@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ObjectToCsv&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ObjectToCsv&version=2.0.0
                    
Install as a Cake Tool

ObjectToCsv

C# Library: Object List to CSV Converter

ObjectToCsv MIT License

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

  1. 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#.
  2. Manual Installation:

    • Download the ObjectToCsv library from NuGet.
    • Add the reference to your project.

Custom Attributes

  1. 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.
  2. 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.

  1. Create a list of Person objects:
  2. Add Header and Ordinal 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
2.0.0 125 4/4/2025
1.3.0 153 3/27/2025
1.2.0 114 1/17/2025
1.1.0 195 4/4/2024
1.0.0 128 4/4/2024