38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using CsvHelper;
|
|
|
|
namespace DTS.Common.Import
|
|
{
|
|
public static class CsvUtil
|
|
{
|
|
public static CsvReader CreateCsvReader(string filename)
|
|
{
|
|
var reader = new StreamReader(filename);
|
|
var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture);
|
|
return csvReader;
|
|
}
|
|
/// <summary>
|
|
/// Read fields from csv reader
|
|
/// </summary>
|
|
/// <param name="csvReader">an instance of csv readere</param>
|
|
/// <param name="readNextLine">determine if read next line and then get the fields or just get fields from the existing state</param>
|
|
/// <returns></returns>
|
|
public static List<string> ReadFields(CsvReader csvReader, bool readNextLine = true)
|
|
{
|
|
List<string> tokens = new List<string>();
|
|
if (readNextLine)
|
|
{
|
|
csvReader.Read();
|
|
}
|
|
for (int i = 0; i < csvReader.ColumnCount; i++)
|
|
{
|
|
tokens.Add(csvReader.GetField(i)?.Trim());
|
|
}
|
|
return tokens;
|
|
}
|
|
|
|
}
|
|
}
|