コード例 #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.");
        }
コード例 #2
0
        /// <summary>Simply dumps the DataTable contents to a delimited file. Only allows to set the delimiter.</summary>
        /// <param name="dt">The source Data Table</param>
        /// <param name="filename">The destination file.</param>
        /// <param name="options">The options used to write the file</param>
        public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options)
        {
            using (var fs = new StreamWriter(filename, false, options.Encoding))
            {
                foreach (DataRow dr in dt.Rows)
                {
                    var fields = dr.ItemArray;

                    for (var i = 0; i < fields.Length; i++)
                    {
                        if (i > 0)
                            fs.Write(options.Delimiter);

                        fs.Write(options.ValueToString(fields[i]));
                    }
                    fs.Write(StringHelper.NewLine);
                }
                fs.Close();
            }
        }
コード例 #3
0
 /// <summary>Reads a Csv File and return their contents as DataTable</summary>
 /// <param name="filename">The file to read.</param>
 /// <param name="options">The options used to create the record mapping class.</param>
 /// <returns>The contents of the file as a DataTable</returns>
 public static DataTable CsvToDataTable(string filename, CsvOptions options)
 {
     var engine = new CsvEngine(options);
     return engine.ReadFileAsDT(filename);
 }
コード例 #4
0
 /// <summary>Reads a Csv File and return their contents as DataTable</summary>
 /// <param name="classname">The name of the record class</param>
 /// <param name="delimiter">The delimiter for each field</param>
 /// <param name="filename">The file to read.</param>
 /// <param name="hasHeader">Indicates if the file contains a header with the field names.</param>
 /// <returns>The contents of the file as a DataTable</returns>
 public static DataTable CsvToDataTable(string filename, string classname, char delimiter, bool hasHeader)
 {
     var options = new CsvOptions(classname, delimiter, filename);
     if (hasHeader == false) options.HeaderLines = 0;
     return CsvToDataTable(filename, options);
 }
コード例 #5
0
 private static Type GetMappingClass(CsvOptions options)
 {
     var cb = new CsvClassBuilder(options);
     return cb.CreateRecordClass();
 }
コード例 #6
0
 /// <summary>Create a CsvEngine using the specified sample file with their headers.</summary>
 /// <param name="options">The options used to create the record mapping class.</param>
 public CsvEngine(CsvOptions options) : base(GetMappingClass(options))
 {
 }