Example #1
0
        /// <summary>
        /// Writes the records to the speficed file using automap and settings.
        /// </summary>
        /// <typeparam name="T">The record type.</typeparam>
        /// <param name="csvFile">The CSV file to write.</param>
        /// <param name="records">The records to write.</param>
        /// <param name="settings">The settings.</param>
        public void WriteRecords <T>(FilePath csvFile, List <T> records, CsvHelperSettings settings)
        {
            if (csvFile == null)
            {
                throw new ArgumentNullException(nameof(csvFile));
            }
            if (records == null)
            {
                throw new ArgumentNullException(nameof(records));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            // Make the path absolute if necessary.
            var file = GetFile(csvFile);

            using (var stream = file.OpenWrite())
                using (var textWriter = new StreamWriter(stream, settings.Encoding))
                    using (var csvWriter = new CsvWriter(textWriter)) {
                        csvWriter.WriteHeader <T>();
                        foreach (var record in records)
                        {
                            csvWriter.WriteRecord(record);
                        }
                        textWriter.Close();
                    }
        }
Example #2
0
        /// <summary>
        /// Writes the records to the speficed file using the specified mapping and settings.
        /// </summary>
        /// <typeparam name="T">The record type.</typeparam>
        /// <param name="csvFile">The CSV file to write.</param>
        /// <param name="records">The records to write.</param>
        /// <param name="mapping">The property column mapping.</param>
        /// <param name="settings">The settings.</param>
        public void WriteRecords <T>(FilePath csvFile, List <T> records, Dictionary <string, string> mapping,
                                     CsvHelperSettings settings)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }
            var customMap = new DefaultClassMap <T>();

            customMap.AutoMap();
            WriteRecords(csvFile, records, customMap, settings);
        }
Example #3
0
        /// <summary>
        /// Reads records from a CSV returning a list of the object type passed. Automapping is used if a Map is configured in settings.
        /// </summary>
        /// <typeparam name="T">The record type.</typeparam>
        /// <param name="csvFile">The CSV file to read.</param>
        /// <param name="classMap">The class map to use, null if not needed.</param>
        /// <param name="settings">The settings.</param>
        /// <returns>List of defined type.</returns>
        public IEnumerable <T> ReadRecords <T>(FilePath csvFile, ClassMap classMap, CsvHelperSettings settings)
        {
            if (csvFile == null)
            {
                throw new ArgumentNullException(nameof(csvFile));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            var file = GetFile(csvFile);

            using (var textReader = new StreamReader(file.OpenRead()))
                using (var csvReader = new CsvReader(textReader)) {
                    if (classMap != null)
                    {
                        csvReader.Configuration.RegisterClassMap(classMap);
                    }
                    return(csvReader.GetRecords <T>().ToList());
                }
        }
Example #4
0
        /// <summary>
        /// Writes the records to the speficed file using the specified class mapp and settings.
        /// </summary>
        /// <typeparam name="T">The record type.</typeparam>
        /// <param name="csvFile">The CSV file to write.</param>
        /// <param name="records">The records to write.</param>
        /// <param name="classMap">The class map.</param>
        /// <param name="settings">The settings.</param>
        public void WriteRecords <T>(FilePath csvFile, List <T> records, ClassMap classMap, CsvHelperSettings settings)
        {
            if (csvFile == null)
            {
                throw new ArgumentNullException(nameof(csvFile));
            }
            if (records == null)
            {
                throw new ArgumentNullException(nameof(records));
            }
            if (classMap == null)
            {
                throw new ArgumentNullException(nameof(classMap));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var file = GetFile(csvFile);

            using (var stream = file.OpenWrite())
                using (var textWriter = new StreamWriter(stream, settings.Encoding))
                    using (var csvWriter = new CsvWriter(textWriter)) {
                        csvWriter.Configuration.RegisterClassMap(classMap);
                        csvWriter.WriteHeader <T>();
                        foreach (var record in records)
                        {
                            csvWriter.WriteRecord(record);
                        }
                        textWriter.Close();
                    }
        }
        public static IEnumerable <T> ReadCsv <T>(this ICakeContext context, FilePath csvFile, ClassMap classMap, CsvHelperSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var csvHelpers = new CsvHelpers(context.FileSystem, context.Environment);

            return(csvHelpers.ReadRecords <T>(csvFile, classMap, settings));
        }
        public static IEnumerable <T> ReadCsv <T>(this ICakeContext context, FilePath csvFile, ClassMap classMap)
        {
            var settings = new CsvHelperSettings();

            return(ReadCsv <T>(context, csvFile, classMap, settings));
        }
 public static IEnumerable <T> ReadCsv <T>(this ICakeContext context, FilePath csvFile, CsvHelperSettings settings)
 {
     return(ReadCsv <T>(context, csvFile, null, settings));
 }
        public static void WriteCsv <T>(this ICakeContext context, FilePath csvFile, List <T> records, ClassMap classMap, CsvHelperSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var csvHelpers = new CsvHelpers(context.FileSystem, context.Environment);

            csvHelpers.WriteRecords(csvFile, records, classMap, settings);
        }
        public static void WriteCsv <T>(this ICakeContext context, FilePath csvFile, List <T> records)
        {
            var settings = new CsvHelperSettings();

            WriteCsv(context, csvFile, records, settings);
        }