Example #1
0
        public CsvHandler(string filePath)
        {
            #region Arrange

            FilePath      = filePath;
            IsHeaderValid = true;
            IsFieldsValid = true;
            HeaderErrors  = new List <string>();
            FieldsErrors  = new List <string>();
            CsvContent    = new List <TModel>();

            #endregion Arrange

            var config = new Configuration.Configuration();
            config.RegisterClassMap <TModelMap>();
            config.HeaderValidated = HeaderValidatedCallback;

            using (var stream = new StreamReader(filePath))
                using (var reader = new CsvReader(stream, config))
                {
                    #region Validate Header

                    reader.Read();
                    reader.ReadHeader();
                    reader.ValidateHeader <TModel>();

                    #endregion Validate Header

                    if (IsHeaderValid && IsFieldsValid)
                    {
                        var records = reader.GetRecords <TModel>().ToList();
                    }
                }
        }
        public byte[] Composer <T, TMapper>(IEnumerable <T> records) where T : class, new() where TMapper : ClassMap <T>
        {
            byte[] result;

            var config = new Configuration.Configuration();

            config.RegisterClassMap <TMapper>();

            using (var ms = new MemoryStream())
            {
                using (var sw = new StreamWriter(ms, Encoding))
                    using (var writer = new CsvWriter(sw, config))
                    {
                        writer.WriteRecords <T>(records);
                    }

                result = ms.ToArray();
            }

            return(result);
        }