Nevron .NET Vision
Chart for .NET / User's Guide / Data Manipulation / Importing / Importing From Csv File

In This Topic
    Importing From Csv File
    In This Topic

    The data contained in the data series can be imported from a csv file. This functionality is exposed to the user via LoadDataTableFromFile and LoadDataTableFromStream methods of NCsvReader class. Note that these methods returns DataTable object, which should be additionally imported to the data series via NDataSeries.FillFromDataTable method or bound using NDataBindingManager.

     Setup NCsvReader properties.

    The user can specify the following properties of the NCsvReader class:

    • CellCeparator - determines what character is used to separate the cells in the csv file. Default value is ",".
    • LineSeparators - a char array which determines a sequence of characters used to separate lines in the csv file. Default value is ";".
      Note, if there is no character at the end of each line into the file the value of LineSeparators has to be {'\r','\n'}.
    • EscapeCharacter - specifies which character is used as escape character in the csv file. Default value is "\".
    • HasHeader - Boolean property which determines whether the first line of the csv file should be consider as headers of the columns. Default value is false.
    • QuotedCellIfContainsDelimiter - Boolean property which determines whether in csv file if the cell contains delimiter character to quote the content of the cell. Default value is true.
    • TrimCell - determines whether to trim the leading and trailing spaces in the cell. Default value is true.
     Specifying the type of the columns read from csv file.

    To be able to read the data from the csv file the user must first specify the expected type of data for each column.

    For example if the file has 3 columns one with string values, one with int values and one with Boolean values the user should add an appropriate column to the NCsvReader.Columns collection.

    Copy Code
    reader.Columns.Add(new NStringCsvColumn("String Column"));
    reader.Columns.Add(new NIntCsvColumn("Integer Column"));
    reader.Columns.Add(new NBooleanCsvColumn("Boolean Column"));
    Visual Basic
    Copy Code
    reader.Columns.Add(New NStringCsvColumn("String Column"))
    reader.Columns.Add(New NIntCsvColumn("Integer Column"))
    reader.Columns.Add(New NBooleanCsvColumn("Boolean Column"))

    The following column types are defined: NBooleanCsvColumn, NColorCsvColumn, NDecimalCsvColumn, NDoubleCsvColumn, NEmptyCsvColumn, NInt32CsvColumn and NStringCsvColumn.

    The user can create its own column types by subclassing the abstract NCsvColumn class.

     Example using NCsvReader class


    Copy Code
    string csvFile = //csv file name.
    NCsvReader reader = new NCsvReader();
    reader.CellSeparator = ',';
    reader.LineSeparators = new char[] { '\r', '\n' };
    reader.HasHeader = false;
    reader.EscapeCharacter = '\\';
    reader.TrimCell = true;
    reader.Columns.Add(new NStringCsvColumn("String Column"));
    reader.Columns.Add(new NDoubleCsvColumn("Double Column"));
    reader.Columns.Add(new NDecimalCsvColumn("Decimal Column"));
    reader.Columns.Add(new NColorCsvColumn("Color Column"));
        DataTable dataTable = reader.LoadDataTableFromFile(csvFile);
    Visual Basic
    Copy Code
    Dim csvFile As String = 'csv file name.
    Dim reader As NCsvReader = New NCsvReader()
    reader.CellSeparator = ","c
    reader.LineSeparators = New char[] { "\r"c, "\n"c }
    reader.HasHeader = False
    reader.EscapeCharacter = "\\"c
    reader.TrimCell = True
    reader.Columns.Add(New NStringCsvColumn("String Column"))
    reader.Columns.Add(New NDoubleCsvColumn("Double Column"))
    reader.Columns.Add(New NDecimalCsvColumn("Decimal Column"))
    reader.Columns.Add(New NColorCsvColumn("Color Column"))
        DataTable dataTable = reader.LoadDataTableFromFile(csvFile)
     Related Examples
    Windows forms: All Examples\Data Manipulation\Importing\From CSV File.