CsvUtility 1.3.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CsvUtility --version 1.3.0
NuGet\Install-Package CsvUtility -Version 1.3.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="CsvUtility" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CsvUtility --version 1.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CsvUtility, 1.3.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.
// Install CsvUtility as a Cake Addin #addin nuget:?package=CsvUtility&version=1.3.0 // Install CsvUtility as a Cake Tool #tool nuget:?package=CsvUtility&version=1.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommonUtil2;
using System.IO.MemoryMappedFiles;
using CsvUtility;
namespace CsvUtilityTest
{
/**
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 buttonHT = new Button() { Parent = this, Dock = DockStyle.Top, Text = "HeaderTest", };
buttonHT.Click += (sender, e) =>
{
OpenFileDialog ofd = new OpenFileDialog() { InitialDirectory = Application.StartupPath, };
if(ofd.ShowDialog() == DialogResult.OK)
{
CsvFileV2 csvFileV2 = CsvFileV2.Open(ofd.FileName);
var idx = csvFileV2.FindExactUniqueHeaderColumnIndex("test");
MessageBox.Show(idx.ToString());
idx = csvFileV2.FindFirstUniqueHeaderColumnIndex("test");
MessageBox.Show(idx.ToString());
}
};
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.
//if you know full column name and that is unique, use FindExactUniqueHeaderColumnIndex
//add using CsvUtility
var nameColumnIdx = csv2.FindFirstUniqueHeaderColumnIndex("name");
//var nameColumnIdx = csv2.FindExactUniqueHeaderColumnIndex("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;
}
foreach (var l in csv2.AllLinesAsArray)
{
List<string> text = new List<string>();
/** Need to confirm array size */
try
{
text.Add(string.Format("{0}={1}", nameColumnIdx, l[nameColumnIdx]));
text.Add(string.Format("{0}={1}", bdColumnIdx, l[bdColumnIdx]));
text.Add(string.Format("{0}={1}", remarkColumnIdx, l[remarkColumnIdx]));
}
catch (Exception exception)
{
text.Add(string.Format("Column not found: {0}", exception.Message));
}
var ret = MessageBox.Show(string.Join(Environment.NewLine, text), "line", MessageBoxButtons.OKCancel);
if (ret == DialogResult.Cancel) break;
}
if (MessageBox.Show("Write data to file?", "Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//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;
}
foreach (var l in csv2.AllLinesAsArray)
{
List<string> text = new List<string>();
/** Need to confirm array size */
try
{
text.Add(string.Format("{0}={1}", nameColumnIdx, l[nameColumnIdx]));
text.Add(string.Format("{0}={1}", bdColumnIdx, l[bdColumnIdx]));
text.Add(string.Format("{0}={1}", remarkColumnIdx, l[remarkColumnIdx]));
}
catch (Exception exception)
{
text.Add(string.Format("Column not found: {0}", exception.Message));
}
var ret = MessageBox.Show(string.Join(Environment.NewLine, text), "line", MessageBoxButtons.OKCancel);
if (ret == DialogResult.Cancel) break;
}
}
}
};
StartPosition = FormStartPosition.CenterScreen;
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.6.1
- 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.4.5 | 136 | 5/8/2024 |
1.4.4 | 119 | 4/27/2024 |
1.4.3 | 165 | 1/23/2024 |
1.4.2 | 219 | 11/27/2023 |
1.4.1 | 139 | 11/26/2023 |
1.4.0 | 139 | 11/25/2023 |
1.3.0 | 180 | 7/16/2023 |
1.2.9 | 165 | 6/30/2023 |
1.2.8.1 | 343 | 11/15/2022 |
1.2.8 | 328 | 11/14/2022 |
1.2.7 | 377 | 10/8/2022 |
1.2.5 | 440 | 7/16/2022 |
1.2.4 | 447 | 7/10/2022 |
1.2.2 | 434 | 6/30/2022 |
1.2.1 | 419 | 6/18/2022 |
1.2.0 | 447 | 5/9/2022 |
1.1.8 | 441 | 4/16/2022 |
1.1.7 | 447 | 3/17/2022 |
1.1.6 | 449 | 2/16/2022 |
1.1.5 | 423 | 2/6/2022 |
1.1.4 | 430 | 1/21/2022 |
1.1.3 | 440 | 1/19/2022 |
1.1.2 | 442 | 1/18/2022 |
1.1.1 | 286 | 12/29/2021 |
1.1.0 | 303 | 11/23/2021 |
1.0.9 | 344 | 10/27/2021 |
1.0.8 | 318 | 10/18/2021 |
1.0.7 | 328 | 10/13/2021 |