Files
DP44/Common/DTS.Common.Import/CsvUtil.cs

38 lines
1.2 KiB
C#
Raw Normal View History

2026-04-17 14:55:32 -04:00
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;
}
}
}