Shos.CsvHelper 1.0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Shos.CsvHelper --version 1.0.0.1                
NuGet\Install-Package Shos.CsvHelper -Version 1.0.0.1                
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="Shos.CsvHelper" Version="1.0.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Shos.CsvHelper --version 1.0.0.1                
#r "nuget: Shos.CsvHelper, 1.0.0.1"                
#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 Shos.CsvHelper as a Cake Addin
#addin nuget:?package=Shos.CsvHelper&version=1.0.0.1

// Install Shos.CsvHelper as a Cake Tool
#tool nuget:?package=Shos.CsvHelper&version=1.0.0.1                

Shos.CsvHelper

Csv (comma-separated values) Library

Projects

Shos.CsvHelper

  • Csv (comma-separated values) Library
  • .NET Standard Library
  • .NET Standard 1.3 or later
  • for .NET Network 4.6 or later, .NET Core 1.1 or later
  • Install as a NuGet Package: NuGet Gallery | Shos.CsvHelper

Shos.CsvHelper.NetFramework

Shos.CsvHelperSample.NetCore

  • .NET Core Console Sample for Shos.CsvHelper

Shos.CsvHelperSample.NetFramework

  • .NET Framework Console Sample for Shos.CsvHelper.NetFramework

Sample

CsvHelperSample.cs | Shos.CsvHelperSample.NetCore or Shos.CsvHelperSample.NetFramework

// .NET Core 1.1 or later
// .NET Framework 4.5.2 or later

namespace Shos.CsvHelperSample
{
    using Shos.CsvHelper;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    class Program
    {
        static void Main() => CsvHelperTester.Run().Wait();
    }

    static class CsvHelperTester
    {
        public static async Task Run()
        {
            // something IEnumerable<TElement>
            // TElement:
            // public properties of TElement will be written and read as csv
            // for writing: type of each property should have "get" and "set"
            // for reading: type of each property should have "get" and "set" and should be string or enum or type which has a default constructor and can "TryParse" or "Parse"

            IEnumerable<ToDo> toDoes = new ToDoList();
            toDoes.Show();

            // set encoding if you need (the default is UTF8)
            CsvSerializer.Encoding = Encoding.GetEncoding(0);

            // set separator if you need (the default is ',')
            //CsvSerializer.Separator = '\t';

            // write csv with header (recommended)
            const string csvWithHeaderFileName = "todo.withheader.csv";
            await toDoes.WriteCsvAsync(csvWithHeaderFileName);

            /*
            Result: todo.withheader.csv

Id,Title,Deadline,Done,Priority,Details,DaySpan
1,filing tax returns,2018/12/01 0:00:00,False,Middle,,0
2,report of a business trip,2017/07/12 13:13:01,False,High,"""ASAP""",3
3,expense slips,2017/07/12 13:13:01,True,Low,"book expenses: ""C# 6.0 and the .NET 4.6 Framework"",""The C# Programming""",0
4, wish list ,2017/07/12 13:13:01,False,High," 	 (1) ""milk""
 	 (2) shampoo
 	 (3) tissue ",0
             */

            IEnumerable<ToDo> newToDoes = await CsvSerializer.ReadCsvAsync<ToDo>(csvFilePathName: csvWithHeaderFileName);
            newToDoes.Show();

            // write csv without header
            const string csvWithoutHeaderFileName = "todo.withoutheader.csv";
            toDoes.WriteCsv(csvFilePathName: csvWithoutHeaderFileName, hasHeader: false);

            /*
            Result: todo.withoutheader.csv

1,filing tax returns,2018/12/01 0:00:00,False,Middle,,0
2,report of a business trip,2017/07/12 13:13:01,False,High,"""ASAP""",3
3,expense slips,2017/07/12 13:13:01,True,Low,"book expenses: ""C# 6.0 and the .NET 4.6 Framework"",""The C# Programming""",0
4, wish list ,2017/07/12 13:13:01,False,High," 	 (1) ""milk""
 	 (2) shampoo
 	 (3) tissue ",0
             */

            newToDoes = CsvSerializer.ReadCsv<ToDo>(csvFilePathName: csvWithoutHeaderFileName, hasHeader: false);
            newToDoes.Show();
        }

        static void Show<TElement>(this IEnumerable<TElement> collection)
        {
            collection.ToList().ForEach(element => Console.WriteLine(element));
            Console.WriteLine();
        }
    }

    class ToDoList : IEnumerable<ToDo> // sample data
    {
        public IEnumerator<ToDo> GetEnumerator()
        {
            yield return new ToDo { Id = 1, Title = "filing tax returns", Deadline = new DateTime(2018, 12, 1) };
            yield return new ToDo { Id = 2, Title = "report of a business trip", Detail = "\"ASAP\"", DaySpan = new DaySpan(3), Priority = Priority.High };
            yield return new ToDo { Id = 3, Title = "expense slips", Detail = "book expenses: \"C# 6.0 and the .NET 4.6 Framework\",\"The C# Programming\"", Priority = Priority.Low, Done = true };
            yield return new ToDo { Id = 4, Title = " wish list ", Detail = " \t (1) \"milk\"\n \t (2) shampoo\n \t (3) tissue ", Priority = Priority.High };
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }

    class ToDo // sample class
    {
        // public properties will be written and read as csv
        // for writing: type of each property should have "get" and "set"
        // for reading: type of each property should have "get" and "set" and should be string or enum or type which has a default constructor and can "TryParse" or "Parse"

        public int      Id       { get; set; }                    // yes (this property will be written and read as csv)
        public string   Title    { get; set; } = "";              // yes
        public DateTime Deadline { get; set; } = DateTime.Now;    // yes
        public bool     Done     { get; set; }                    // yes
        public Priority Priority { get; set; } = Priority.Middle; // yes: user-defined enum
        [ColumnName("Details")]
        public string   Detail   { get; set; } = "";              // yes: change column name with [ColumnName("Details")]
        public DaySpan  DaySpan  { get; set; }                    // yes: user-defined type which can't "TryParse" but "Parse"
        [CsvIgnore()]
        public string   Option   { get; set; } = "";              // no : ignore this property with [CsvIgnore()]
        public string   Version => "1.0";                         // no : read only or write only property will be ignored

        public override string ToString()
            => $"Id: {Id}, Title: {Title}, Deadline: {Deadline.ToString()}, Done: {Done}, Priority: {Priority}, Detail: {Detail}, DaySpan: {DaySpan}";
    }

    enum Priority { High, Middle, Low } // sample enum

    // sample type which can't "TryParse" but "Parse"
    struct DaySpan
    {
        public int Value { get; private set; }

        public DaySpan(int value) => Value = value;
        public static DaySpan Parse(string text) => new DaySpan(int.Parse(text));
        public override string ToString() => Value.ToString();
    }
}

Author Info

Fujio Kojima: a software developer in Japan

  • Microsoft MVP for Development Tools - Visual C# (Jul. 2005 - Dec. 2014)
  • Microsoft MVP for .NET (Jan. 2015 - Oct. 2015)
  • Microsoft MVP for Visual Studio and Development Technologies (Nov. 2015 - Jun. 2018)
  • Microsoft MVP for Developer Technologies (Nov. 2018 - Jun. 2021)
  • MVP Profile
  • Blog (Japanese)
  • Web Site (Japanese)
  • Twitter
  • Instagram

License

This library is under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.
  • .NETStandard 2.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
1.0.0.3 152 3/26/2024
1.0.0.2 742 5/21/2021
1.0.0.1 773 9/18/2019
1.0.0 536 9/12/2019
0.1.1.2 646 3/8/2019
0.1.1.1 1,174 7/13/2017
0.1.1 1,059 7/12/2017
0.1.0.9 1,094 7/12/2017
0.1.0.8 1,082 7/10/2017
0.1.0.6 1,057 7/7/2017
0.1.0.4 1,031 7/7/2017
0.1.0 1,132 6/22/2017