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:
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.
C# |
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
C# |
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"));
try
{
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"))
Try
DataTable dataTable = reader.LoadDataTableFromFile(csvFile)
|
Related Examples
Windows forms: All Examples\Data Manipulation\Importing\From CSV File.