Inheritance: IDisposable
Ejemplo n.º 1
0
        public static DataTable ReadFromCSV(string fullFilePath)
        {
            DataTable table = new DataTable();

            using (CSVReader reader = new CSVReader(fullFilePath))
            {
                // Create columns.
                ColumnName[] columns = new ColumnName[reader.TitlePositions.Count];
                int columnIndex = 0;

                foreach (KeyValuePair<string, int> titleInfo in reader.TitlePositions.OrderBy(p => p.Value))
                {
                    columns[columnIndex++] = table.AddColumn(titleInfo.Key);
                }

                // Iterate through each row in the CSV file and add the data to the table.
                for (int rowIndex = 0; rowIndex < reader.Length(); rowIndex++)
                {
                    // Create a new row.
                    Row row = table.AppendRow();
                    for(columnIndex = 0; columnIndex < columns.Length; columnIndex++)
                    {
                        ColumnName columnName = columns[columnIndex];
                        row[columnName] = reader.GetValue(rowIndex, columnName.Name);
                    }
                }
            }

            return table;
        }