Exemplo n.º 1
0
        /// <summary>Creates a new DelimitedClassBuilder.</summary>
        /// <param name="options">The specifications for the Csv file.</param>
        public CsvClassBuilder(CsvOptions options) : base(options.RecordClassName, options.Delimiter.ToString())
        {
            IgnoreFirstLines = 1;

            if (options.SampleFileName != string.Empty)
            {
                var firstLine = CommonEngine.RawReadFirstLines(options.SampleFileName, 1);

                if (options.HeaderLines > 0)
                {
                    foreach (var header in firstLine.Split(options.HeaderDelimiter == char.MinValue ? options.Delimiter : options.HeaderDelimiter))
                    {
                        AddField(StringToIdentifier(header));
                    }
                }
                else
                {
                    var fieldsNbr = firstLine.Split(options.Delimiter).Length;
                    for (var i = 0; i < fieldsNbr; i++)
                    {
                        AddField(options.FieldsPrefix + i.ToString());
                    }
                }
            }
            else if (options.NumberOfFields > 0)
            {
                AddFields(options.NumberOfFields, options.FieldsPrefix);
            }
            else
            {
                throw new BadUsageException("You must provide a SampleFileName or a NumberOfFields to parse a genric CSV file.");
            }
        }