CsvUtility 1.2.5

.NET Framework 4.5.2
NuGet\Install-Package CsvUtility -Version 1.2.5
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.
dotnet add package CsvUtility --version 1.2.5
<PackageReference Include="CsvUtility" Version="1.2.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CsvUtility --version 1.2.5
#r "nuget: CsvUtility, 1.2.5"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install CsvUtility as a Cake Addin
#addin nuget:?package=CsvUtility&version=1.2.5

// Install CsvUtility as a Cake Tool
#tool nuget:?package=CsvUtility&version=1.2.5
/**
Paste below and save as .csv

seq,id,name,gender,birthday,remark
0,10001,Foo,male,1970/10/1,
1,10002,Bar,male,1980/2/1,test
2,10003,John,male,1990/3/15,
3,10004,Sophia,female,2000/12/5,

Or,
"seq","id","name","gender","birthday","remark"
"0","10001","Foo","male","1970/10/1",""
"1","10002","Bar","male","1980/2/1","test"
"2","10003","John","male","1990/3/15",""
"3","10004","Sophia","female","2000/12/5",

Also can use \t insted of comma
*/

public partial class CsvUtilityTest : Form
{
    public CsvUtilityTest()
    {
        Button button = new Button() { Parent = this, Dock = DockStyle.Top, Text = "OpenAndCreate", };
        button.Click += (sender, e) =>
        {
            OpenFileDialog ofd = new OpenFileDialog() { InitialDirectory = Application.StartupPath, };

            //select book1.csv or book1dq.csv
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                var csv2 = CsvFileV2.Open(ofd.FileName);
                var debuginfo = new List<string>();
                debuginfo.Add(string.Format("File name: {0}", csv2.FileName));
                debuginfo.Add(string.Format("Delimiter: {0}", csv2.Delimiter));
                debuginfo.Add(string.Format("Line count: {0}", csv2.LineCount));
                debuginfo.Add(string.Format("Encoding: {0}", csv2.Encoding));
                debuginfo.Add(string.Empty);

                debuginfo.Add("-- Header list --");
                debuginfo.AddRange(csv2.UniqueHeader);

                MessageBox.Show(string.Join(Environment.NewLine, debuginfo));

                //if column number is not sure, find it by using part of column name
                //add using CsvUtility
                var nameColumnIdx = csv2.FindFirstUniqueHeaderColumnIndex("name");
                var nameColumnName = csv2.FindFirstUniqueHeaderColumnName("name");
                var bdColumnIdx = csv2.FindFirstUniqueHeaderColumnIndex("birthday");
                var bdColumnName = csv2.FindFirstUniqueHeaderColumnName("birthday");
                var remarkColumnIdx = csv2.FindFirstUniqueHeaderColumnIndex("remark");
                var remarkColumnName = csv2.FindFirstUniqueHeaderColumnName("remark");

                foreach (var l in csv2.AllLines)
                {
                    List<string> text = new List<string>();
                    text.Add(string.Format("{0}={1}", nameColumnIdx, l.GetValue(nameColumnIdx)));
                    text.Add(string.Format("{0}={1}", nameColumnName, l.GetValue(nameColumnName)));
                    text.Add(string.Format("{0}={1}", bdColumnIdx, l.GetValueAsDate(bdColumnIdx)));
                    text.Add(string.Format("{0}={1}", bdColumnName, l.GetValueAsDate(bdColumnName)));
                    text.Add(string.Format("{0}={1}", remarkColumnIdx, l.GetValue(remarkColumnIdx)));
                    text.Add(string.Format("{0}={1}", remarkColumnName, l.GetValue(remarkColumnName)));

                    /** if you are sure a correct name, can get a value using the key directly. */
                    if (l.ContainsKey("2: name"))
                    {
                        Console.WriteLine(l["2: name"]);
                    }

                    var ret = MessageBox.Show(string.Join(Environment.NewLine, text), "line", MessageBoxButtons.OKCancel);
                    if (ret == DialogResult.Cancel) break;
                }

                foreach (var l in csv2.AllLinesWithLineNumber)
                {
                    List<string> text = new List<string>();
                    text.Add(string.Format("{0}={1}", nameColumnIdx, l.Value.GetValue(nameColumnIdx)));
                    text.Add(string.Format("{0}={1}", nameColumnName, l.Value.GetValue(nameColumnName)));
                    text.Add(string.Format("{0}={1}", bdColumnIdx, l.Value.GetValueAsDate(bdColumnIdx)));
                    text.Add(string.Format("{0}={1}", bdColumnName, l.Value.GetValueAsDate(bdColumnName)));
                    text.Add(string.Format("{0}={1}", remarkColumnIdx, l.Value.GetValue(remarkColumnIdx)));
                    text.Add(string.Format("{0}={1}", remarkColumnName, l.Value.GetValue(remarkColumnName)));


                    var ret = MessageBox.Show(string.Join(Environment.NewLine, text), string.Format("Current line number: {0}", l.Key), MessageBoxButtons.OKCancel);
                    if (ret == DialogResult.Cancel) break;
                }

                //create a csv file
                var csvCreatefilename = string.Format(".\\{0:yyyyMMdd_HHmmss}.csv", DateTime.Now);
                var csvCreate = CsvFileV2.Create(csvCreatefilename, new[] { "name", "modify date" });

                /** Require unique column name */
                var csvCreateNameColumnName = csvCreate.FindFirstUniqueHeaderColumnName("name");
                var csvCreateModifyDateColumnName = csvCreate.FindFirstUniqueHeaderColumnName("modify date");

                var ln = 0;
                foreach (var l in csv2.AllLines)
                {
                    /** There are two ways for adding line. */
                    if (++ln % 2 == 1)
                    {
                        /** Get a line template of the csv file. */
                        var tempdic = csvCreate.CreateLineTemplate();

                        /** Insert value to a column which update required. */
                        tempdic[csvCreateNameColumnName] = l.GetValue(nameColumnIdx);
                        tempdic[csvCreateModifyDateColumnName] = DateTime.Now;

                        /** Add line. */
                        csvCreate.AddLine(tempdic);
                    }
                    else
                    {
                        /** Add line by specifying all values. */
                        csvCreate.AddLine(l.GetValue(nameColumnIdx), DateTime.Now);
                    }
                    l["name"] = "AA";
                }

                csvCreate.Save();
                MessageBox.Show(string.Format("wrote to {0}.", csvCreatefilename));

                /** Change delimiter. Also can specify when create. */
                csvCreate.Delimiter = eDelimiter.Tab;
                csvCreatefilename = string.Format(".\\{0:yyyyMMdd_HHmmss}.csv", DateTime.Now);
                /** Change file name.*/
                csvCreate.Save(csvCreatefilename);
                MessageBox.Show(string.Format("wrote to {0} as tab split file.", csvCreatefilename));


            }

        };

        Button buttonParse = new Button() { Parent = this, Dock = DockStyle.Top, Text = "Parse", };
        buttonParse.Click += (sender, e) =>
        {
            OpenFileDialog ofd = new OpenFileDialog() { InitialDirectory = Application.StartupPath, };

            //select book1.csv or book1dq.csv
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //thought as utf8
                using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
                {
                    var csv2 = CsvFileV2.Parse(sr.ReadToEnd());
                    var debuginfo = new List<string>();
                    debuginfo.Add(string.Format("File name: {0}", csv2.FileName));
                    debuginfo.Add(string.Format("Delimiter: {0}", csv2.Delimiter));
                    debuginfo.Add(string.Format("Line count: {0}", csv2.LineCount));
                    debuginfo.Add(string.Format("Encoding: {0}", csv2.Encoding));
                    debuginfo.Add(string.Empty);

                    debuginfo.Add("-- Header list --");
                    debuginfo.AddRange(csv2.UniqueHeader);

                    MessageBox.Show(string.Join(Environment.NewLine, debuginfo));

                    //if column number is not sure, find it by using part of column name
                    //add using CsvUtility
                    var nameColumnIdx = csv2.FindFirstUniqueHeaderColumnIndex("name");
                    var nameColumnName = csv2.FindFirstUniqueHeaderColumnName("name");
                    var bdColumnIdx = csv2.FindFirstUniqueHeaderColumnIndex("birthday");
                    var bdColumnName = csv2.FindFirstUniqueHeaderColumnName("birthday");
                    var remarkColumnIdx = csv2.FindFirstUniqueHeaderColumnIndex("remark");
                    var remarkColumnName = csv2.FindFirstUniqueHeaderColumnName("remark");

                    foreach (var l in csv2.AllLines)
                    {
                        List<string> text = new List<string>();
                        text.Add(string.Format("{0}={1}", nameColumnIdx, l.GetValue(nameColumnIdx)));
                        text.Add(string.Format("{0}={1}", nameColumnName, l.GetValue(nameColumnName)));
                        text.Add(string.Format("{0}={1}", bdColumnIdx, l.GetValueAsDate(bdColumnIdx)));
                        text.Add(string.Format("{0}={1}", bdColumnName, l.GetValueAsDate(bdColumnName)));
                        text.Add(string.Format("{0}={1}", remarkColumnIdx, l.GetValue(remarkColumnIdx)));
                        text.Add(string.Format("{0}={1}", remarkColumnName, l.GetValue(remarkColumnName)));

                        /** if you are sure a correct name, can get a value using the key directly. */
                        if (l.ContainsKey("2: name"))
                        {
                            Console.WriteLine(l["2: name"]);
                        }

                        var ret = MessageBox.Show(string.Join(Environment.NewLine, text), "line", MessageBoxButtons.OKCancel);
                        if (ret == DialogResult.Cancel) break;
                    }

                    foreach (var l in csv2.AllLinesWithLineNumber)
                    {
                        List<string> text = new List<string>();
                        text.Add(string.Format("{0}={1}", nameColumnIdx, l.Value.GetValue(nameColumnIdx)));
                        text.Add(string.Format("{0}={1}", nameColumnName, l.Value.GetValue(nameColumnName)));
                        text.Add(string.Format("{0}={1}", bdColumnIdx, l.Value.GetValueAsDate(bdColumnIdx)));
                        text.Add(string.Format("{0}={1}", bdColumnName, l.Value.GetValueAsDate(bdColumnName)));
                        text.Add(string.Format("{0}={1}", remarkColumnIdx, l.Value.GetValue(remarkColumnIdx)));
                        text.Add(string.Format("{0}={1}", remarkColumnName, l.Value.GetValue(remarkColumnName)));

                        var ret = MessageBox.Show(string.Join(Environment.NewLine, text), string.Format("Current line number: {0}", l.Key), MessageBoxButtons.OKCancel);
                        if (ret == DialogResult.Cancel) break;
                    }
                }
            }
        };


        StartPosition = FormStartPosition.CenterScreen;
    }
}
Product Versions
.NET Framework net452 net46 net461 net462 net463 net47 net471 net472 net48
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

This package has 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.2.5 90 7/16/2022
1.2.4 102 7/10/2022
1.2.2 86 6/30/2022
1.2.1 77 6/18/2022
1.2.0 89 5/9/2022
1.1.8 94 4/16/2022
1.1.7 94 3/17/2022
1.1.6 95 2/16/2022
1.1.5 87 2/6/2022
1.1.4 99 1/21/2022
1.1.3 103 1/19/2022
1.1.2 94 1/18/2022
1.1.1 105 12/29/2021
1.1.0 129 11/23/2021
1.0.9 182 10/27/2021
1.0.8 159 10/18/2021
1.0.7 140 10/13/2021