Exemplo n.º 1
0
 internal CsvRow(int rowNumber, string line, CsvCellRange[] ranges, Dictionary <string, int> headerLookup, CsvReaderSettings settings)
 {
     _lookup        = headerLookup;
     _line          = line;
     _ranges        = ranges;
     this.Length    = ranges.Length;
     this.RowNumber = rowNumber;
     _settings      = settings;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new CsvReader instance
        /// </summary>
        /// <param name="input">Input stream containing the csv file</param>
        /// <param name="config"></param>
        public CsvReader(Stream input, CsvReaderSettings readerConfig = null)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (input.CanSeek)
            {
                _streamInitialPosition = input.Position;
            }

            if (readerConfig == null)
            {
                _config = new CsvReaderSettings();
            }
            else
            {
                _config = readerConfig;
            }

            _reader           = new StreamReader(input, Encoding.UTF8, true, _config.ReadBufferSize, !_config.DisposeStream);
            _columnNameLookup = new Dictionary <string, int>(_config.ColumnNameComparer);

            _firstRow = this.ReadRows(attemptToYieldFromCache: false).FirstOrDefault();

            // Assume that the first row is the expected number of columns unless specified.

            if (_firstRow == null)
            {
                throw new CsvReaderException("Failed to read first row from csv file.");
            }

            _columnCount = _firstRow.Length;

            string[] columnNames = null;


            // If we expect headers, set the column names to the values of the first row.
            // Otherwise generate column names col1,col2,...
            if (_config.HasHeaders)
            {
                if (_config.TrimColumnNames)
                {
                    columnNames = _firstRow.StringColumns.Select(c => c.Trim()).ToArray();
                }
                else
                {
                    columnNames = _firstRow.StringColumns.ToArray();
                }
            }
            else
            {
                int startAt = _config.IncludeRowNumberAsColumn ? 0 : 1;
                columnNames = Enumerable.Range(startAt, _columnCount).Select(i => "col" + i).ToArray();
            }

            if (columnNames == null || columnNames.Length == 0)
            {
                throw new CsvReaderException("Unable to detect any cells");
            }

            if (_config.IncludeRowNumberAsColumn)
            {
                columnNames[0] = _config.RowNumberColumnName;
            }

            this.ColumnNames = columnNames;
        }
Exemplo n.º 3
0
 public CsvReader(string inputFilePath, CsvReaderSettings config = null) :
     this(new FileStream(inputFilePath, FileMode.Open), config)
 {
 }