CSVFileReader

Implementa la lettura di un file in formato CSV.

Esempi

L’esempio seguente mostra come leggere l’intero contenuto di un documento.

var csvPath = "Test.csv";

try
{
    using (var csvReader = new CSVFileReader(csvPath) { FieldDelimiter = ','})
    {
        if (csvReader.EndOfFile())
        {
            Log.Error("The file " + csvPath + " is empty");
            return;
        }

        var fileLines = csvReader.ReadAll();
        if (fileLines.Count == 0 || fileLines[0].Count == 0)
            return;
    }
}
catch (Exception e)
{
    Log.Error("Unable to read csv file " + csvPath + ": " + e.Message);
}

Il secondo codice mostra invece come leggere un documento riga per riga.

var csvPath = "Test.csv";

try
{
    using (var csvReader = new CSVFileReader(csvPath) { FieldDelimiter = ';', IgnoreMalformedLines = true })
    {
        if (csvReader.EndOfFile())
            return;

        var header = csvReader.ReadLine();
        if (header == null || header.Count == 0)
            return;

        while (!csvReader.EndOfFile())
        {
            var parameters = csvReader.ReadLine();

            if (parameters.Count != header.Count)
            {
                // invalid line
                continue;
            }
        }
    }
}
catch (Exception e)
{
    Log.Error("Unable to read csv file " + csvPath + ": " + e.Message);
}