Beispiel #1
0
 /// <summary>
 /// Read in a single CSV file into a datatable in memory
 /// </summary>
 /// <param name="filename"></param>
 /// <param name="delim">The CSV field delimiter character.</param>
 /// <param name="qual">The CSV text qualifier character.</param>
 /// <returns>An data table of strings that were retrieved from the CSV file.</returns>
 public static DataTable LoadDataTable(StreamReader stream, string[] headers, bool ignore_dimension_errors = true, char delim = CSV.DEFAULT_DELIMITER, char qual = CSV.DEFAULT_QUALIFIER)
 {
     using (CSVReader cr = new CSVReader(stream, delim, qual)) {
         return(cr.ReadAsDataTable(false, ignore_dimension_errors, headers));
     }
 }
Beispiel #2
0
 /// <summary>
 /// Read in a single CSV file as an array of objects
 /// </summary>
 /// <typeparam name="T">The type of objects to deserialize from this CSV.</typeparam>
 /// <param name="stream">The stream to read.</param>
 /// <param name="ignore_dimension_errors">Set to true if you wish to ignore rows that have a different number of columns.</param>
 /// <param name="ignore_bad_columns">Set to true if you wish to ignore column headers that don't match up to object attributes.</param>
 /// <param name="ignore_type_conversion_errors">Set to true if you wish to overlook elements in the CSV array that can't be properly converted.</param>
 /// <param name="delim">The CSV field delimiter character.</param>
 /// <param name="qual">The CSV text qualifier character.</param>
 /// <returns>An array of objects that were retrieved from the CSV file.</returns>
 public static List <T> LoadArray <T>(StreamReader stream, bool ignore_dimension_errors = true, bool ignore_bad_columns = true, bool ignore_type_conversion_errors = true, char delim = CSV.DEFAULT_DELIMITER, char qual = CSV.DEFAULT_QUALIFIER) where T : class, new()
 {
     using (CSVReader cr = new CSVReader(stream, delim, qual)) {
         return(cr.Deserialize <T>(ignore_dimension_errors, ignore_bad_columns, ignore_type_conversion_errors));
     }
 }
Beispiel #3
0
        /// <summary>
        /// Take a CSV file and chop it into multiple chunks of a specified maximum size.
        /// </summary>
        /// <param name="filename">The input filename to chop</param>
        /// <param name="out_folder">The folder where the chopped CSV will be saved</param>
        /// <param name="maxLinesPerFile">The maximum number of lines to put into each file</param>
        /// <param name="settings">The CSV settings to use when chopping this file into chunks (Default: CSV)</param>
        /// <returns>Number of files chopped</returns>
        public static int ChopFile(string filename, string out_folder, int maxLinesPerFile, CSVSettings settings = null)
        {
            // Default settings
            if (settings == null)
            {
                settings = CSVSettings.CSV;
            }

            // Let's begin parsing
            var          file_id     = 1;
            var          line_count  = 0;
            var          file_prefix = Path.GetFileNameWithoutExtension(filename);
            var          ext         = Path.GetExtension(filename);
            CSVWriter    cw          = null;
            StreamWriter sw          = null;

            // Read in lines from the file
            using (var sr = new StreamReader(filename))
            {
                using (var cr = new CSVReader(sr, settings))
                {
                    // Okay, let's do the real work
                    foreach (var line in cr.Lines())
                    {
                        // Do we need to create a file for writing?
                        if (cw == null)
                        {
                            var fn = Path.Combine(out_folder, file_prefix + file_id.ToString() + ext);
                            var fs = new FileStream(fn, FileMode.CreateNew);
                            sw = new StreamWriter(fs, settings.Encoding);
                            cw = new CSVWriter(sw, settings);
                            if (settings.HeaderRowIncluded)
                            {
                                cw.WriteLine(cr.Headers);
                            }
                        }

                        // Write one line
                        cw.WriteLine(line);

                        // Count lines - close the file if done
                        line_count++;
                        if (line_count >= maxLinesPerFile)
                        {
                            cw.Dispose();
                            cw = null;
                            file_id++;
                            line_count = 0;
                        }
                    }
                }
            }

            // Ensure the final CSVWriter is closed properly
            if (cw != null)
            {
                cw.Dispose();
                cw = null;
            }
            return(file_id);
        }