Пример #1
0
 public static byte[] GetCsvBytes(this IEnum data, CsvConfig config = null)
 {
     using (var ms = new MemoryStream())
     {
         data.WriteCsvTo(ms, config);
         return(ms.ToArray());
     }
 }
Пример #2
0
        public void CircularDependencyWithMultiplePropertiesTest()
        {
            var config = new CsvHelper.Configuration.Configuration();
            var map    = config.AutoMap <A>();

            Assert.AreEqual(1, map.MemberMaps.Count);
            Assert.AreEqual(3, map.ReferenceMaps.Count);
        }
Пример #3
0
        public CompletedApplicationScreening ParseApplicationResponse(string businessFileContent, string associatesFileContent)
        {
            CsvHelper.Configuration.Configuration config = new CsvHelper.Configuration.Configuration();
            config.SanitizeForInjection = true;
            config.IgnoreBlankLines     = true;

            config.TrimOptions      = CsvHelper.Configuration.TrimOptions.Trim;
            config.ShouldSkipRecord = record =>
            {
                return(record.All(string.IsNullOrEmpty));
            };

            // fix for unexpected spaces in header
            config.PrepareHeaderForMatch = (string header, int index) => header = header.Trim().ToLower();

            TextReader businessTextReader = new StringReader(businessFileContent);
            var        businessCsv        = new CsvReader(businessTextReader, config);

            businessCsv.Configuration.RegisterClassMap <CsvBusinessImportMap>();

            TextReader associatesTextReader = new StringReader(associatesFileContent);
            var        associatesCsv        = new CsvReader(associatesTextReader, config);

            associatesCsv.Configuration.RegisterClassMap <CsvAssociateImportMap>();

            try
            {
                CsvBusinessImport         businessImport   = businessCsv.GetRecords <CsvBusinessImport>().ToList().First();
                List <CsvAssociateImport> associatesImport = associatesCsv.GetRecords <CsvAssociateImport>().ToList();


                CompletedApplicationScreening response = new CompletedApplicationScreening()
                {
                    RecordIdentifier = businessImport.LcrbBusinessJobId.PadLeft(6, '0'),
                    Result           = CsvBusinessImport.TranslateStatus(businessImport.Result),
                    Associates       = new List <Associate>()
                };
                foreach (var associate in associatesImport)
                {
                    response.Associates.Add(new Associate()
                    {
                        SpdJobId   = associate.LcrbAssociateJobId,
                        LastName   = associate.Last,
                        FirstName  = associate.First,
                        MiddleName = associate.Middle
                    });
                }
                return(response);
            }
            catch (Exception e)
            {
                _logger.LogError("Error parsing worker response.");
                _logger.LogError("Message:");
                _logger.LogError(e.Message);
                // return an empty list so we continue processing other files.
                return(new CompletedApplicationScreening());
            }
        }
Пример #4
0
 public static void WriteCsv(this IEnum data, Stream stream, CsvCfg config = null)
 {
     config = config ?? Configuration.Default;
     using (var sw = new StreamWriter(stream, config.Encoding))
     {
         var csvWriter = new CsvWriter(sw, config);
         csvWriter.WriteRecords(data);
     }
 }
Пример #5
0
        protected virtual CsvHelper.Configuration.Configuration GetConfiguration()
        {
            var configuration = new CsvHelper.Configuration.Configuration
            {
                Delimiter = Delimiter
            };

            return(configuration);
        }
Пример #6
0
 public static void WriteCsvTo(this IEnum data, Stream stream, CsvConfig config = null)
 {
     config = config ?? GetExcelAtDeConfig();
     using (var sw = new StreamWriter(stream, config.Encoding))
     {
         var csvWriter = new CsvWriter(sw, config);
         csvWriter.WriteRecords(data);
     }
 }
Пример #7
0
 public static IList <T> ReadCsv <T>(this FileInfo file, CsvCfg config = null)
 {
     config = config ?? Configuration.Default;
     using (var fs = File.OpenRead(file.FullName))
         using (var sr = new StreamReader(fs, config.Encoding))
         {
             var csvReader = new CsvReader(sr, config);
             return(csvReader.GetRecords <T>().ToList());
         }
 }
Пример #8
0
 public void InitCsvHelperConfig()
 {
     cfg = new CsvHelper.Configuration.Configuration()
     {
         HasHeaderRecord   = true,
         IgnoreBlankLines  = true,
         HeaderValidated   = null,
         MissingFieldFound = null,
     };
 }
Пример #9
0
 public static IList <T> ReadCsv <T>(string fileName, CsvConfig config = null)
 {
     config = config ?? GetExcelAtDeConfig();
     using (var fs = File.OpenRead(fileName))
         using (var sr = new StreamReader(fs, config.Encoding))
         {
             var csvReader = new CsvReader(sr, config);
             return(csvReader.GetRecords <T>().ToList());
         }
 }
Пример #10
0
        public List <CompletedWorkerScreening> ParseWorkerResponse(string fileContent)
        {
            CsvHelper.Configuration.Configuration config = new CsvHelper.Configuration.Configuration();
            config.SanitizeForInjection = true;
            config.IgnoreBlankLines     = true;

            config.TrimOptions      = CsvHelper.Configuration.TrimOptions.Trim;
            config.ShouldSkipRecord = record =>
            {
                return(record.All(string.IsNullOrEmpty));
            };

            // fix for unexpected spaces in header
            config.PrepareHeaderForMatch = (string header, int index) => header = header.Trim().ToLower();

            TextReader textReader = new StringReader(fileContent);
            var        workerCsv  = new CsvReader(textReader, config);

            workerCsv.Configuration.RegisterClassMap <CsvWorkerImportMap>();

            try
            {
                List <CsvWorkerImport>          imports   = workerCsv.GetRecords <CsvWorkerImport>().ToList();
                List <CompletedWorkerScreening> responses = new List <CompletedWorkerScreening>();
                foreach (var import in imports)
                {
                    if (import.RecordIdentifier.Substring(0, 2) == "WR")
                    {
                        responses.Add(new CompletedWorkerScreening()
                        {
                            RecordIdentifier = import.RecordIdentifier,
                            ScreeningResult  = CsvWorkerImport.TranslateStatus(import.Result)
                        });
                    }
                    else
                    {
                        responses.Add(new CompletedWorkerScreening()
                        {
                            SpdJobId        = import.RecordIdentifier,
                            ScreeningResult = CsvWorkerImport.TranslateStatus(import.Result)
                        });
                    }
                }

                return(responses);
            }
            catch (Exception e)
            {
                _logger.LogError("Error parsing worker response.");
                _logger.LogError("Message:");
                _logger.LogError(e.Message);
                // return an empty list so we continue processing other files.
                return(new List <CompletedWorkerScreening>());
            }
        }
Пример #11
0
 private static CsvHelper.Configuration.Configuration GetConfiguration()
 {
     if (CsvConfiguration == null)
     {
         CsvConfiguration = new CsvHelper.Configuration.Configuration(System.Globalization.CultureInfo.InvariantCulture)
         {
             MemberTypes = CsvHelper.Configuration.MemberTypes.Fields,
         };
     }
     return(CsvConfiguration);
 }
        public void CircularDependencyTest()
        {
            var config = new CsvHelper.Configuration.Configuration();
            var map    = config.AutoMap <ACircular>();

            Assert.IsNotNull(map);
            Assert.AreEqual(1, map.MemberMaps.Count);
            Assert.AreEqual(1, map.ReferenceMaps.Count);
            Assert.AreEqual(1, map.ReferenceMaps[0].Data.Mapping.MemberMaps.Count);
            Assert.AreEqual(0, map.ReferenceMaps[0].Data.Mapping.ReferenceMaps.Count);
        }
Пример #13
0
 /// <summary>
 /// Sauvegarde d'une liste typé dans un fichier CSV
 /// </summary>
 public void Write_CSV <T>(string _path, List <T> _myList) where T : class
 {
     CsvHelper.Configuration.Configuration CsvConfig = new CsvHelper.Configuration.Configuration {
         Delimiter = ";"
     };
     using (TextWriter textwritter = new StreamWriter(_path, true, Encoding.GetEncoding(1252)))
     {
         var csv = new CsvWriter(textwritter, CsvConfig);
         csv.WriteRecords(_myList);
     }
 }
Пример #14
0
        private IEnumerable <Vol> GetVols()
        {
            var csvConfig = new CsvHelper.Configuration.Configuration();

            csvConfig.Delimiter = "|";
            using (var fileReader = System.IO.File.OpenText(HttpContext.Server.MapPath(@"~/App_Data/VolData.csv")))
                using (var csvReader = new CsvHelper.CsvReader(fileReader, csvConfig))
                {
                    return(csvReader.GetRecords <Vol>().ToList());
                }
        }
Пример #15
0
        private CsvReader GetCsvHelperReader(Resources.FileSize fileSize)
        {
            var config = new CsvHelper.Configuration.Configuration()
            {
                Delimiter    = Resources.GetSeperator(fileSize).ToString(),
                BadDataFound = (data) => Debug.WriteLine($"Bad Data Found: {data}")
            };

            var stream = new StreamReader(Resources.GetStream(fileSize));

            return(new CsvReader(stream, config));
        }
 public void EnsureInternalsAreSetupWhenPassingWriterAndConfigTest()
 {
     using (var stream = new MemoryStream())
         using (var writer = new StreamWriter(stream))
         {
             var config = new CsvHelper.Configuration.Configuration();
             using (var csv = new CsvWriter(writer, config))
             {
                 Assert.AreSame(config, csv.Configuration);
             }
         }
 }
        public void Setup()
        {
            _csvConfig = new Configuration();
            _csvConfig.RegisterClassMap <CsvHelperMappingForCustomObject>();

            _fluentEngine = new DelimitedFileEngineFactory()
                            .GetEngine(new FlatFileMappingForCustomObject());

            var fixture = new Fixture();

            _records = fixture.CreateMany <CustomObject>(N).ToArray();
        }
 public void EnsureInternalsAreSetupWhenPassingReaderAndConfigTest()
 {
     using (var stream = new MemoryStream())
         using (var reader = new StreamReader(stream))
         {
             var config = new CsvHelper.Configuration.Configuration();
             using (var parser = new CsvParser(reader, config))
             {
                 Assert.AreSame(config, parser.Configuration);
             }
         }
 }
Пример #19
0
        private static void RunOutputCSV(string destinationDir, bool v, List <string> logfiles)
        {
            // Create a csv configuration objection with a custom map to enable milliseconds in the dateTime field
            CsvHelper.Configuration.Configuration csvconf = new CsvHelper.Configuration.Configuration();
            csvconf.RegisterClassMap(new LogLineMap());

            // Determine Hostname
            string host;

            if (logfiles[0].Substring(0, 1) == "\\")
            {
                // First character of log paths is \ (Remote Hostname)
                host = logfiles[0].Split('\\')[2];
            }
            else
            {
                host = System.Environment.MachineName;
            }

            if (v == false)
            {
                // Single CSV
                List <LogLine> loglines = new List <LogLine>();
                Parallel.ForEach(logfiles, (log) =>
                {
                    loglines.AddRange(ParseLogFile(log, host));
                });
                string       outfilename = destinationDir + "\\AllLogs.csv";
                StreamWriter sw          = new StreamWriter(outfilename);
                CsvWriter    csv         = new CsvWriter(sw, csvconf);
                csv.WriteRecords(loglines);
                sw.Close();
            }
            else
            {
                // Multiple CSVs
                Parallel.ForEach(logfiles, (log) =>
                {
                    List <LogLine> loglines = ParseLogFile(log, host);
                    if (loglines.Count > 0)
                    {
                        string[] filenamesplit = log.Split('\\');
                        string filename        = filenamesplit[filenamesplit.Length - 1];
                        filename           = filename.Replace(".log", ".csv");
                        string outfilename = destinationDir + "\\" + filename;
                        StreamWriter sw    = new StreamWriter(outfilename);
                        CsvWriter csv      = new CsvWriter(sw);
                        csv.WriteRecords(loglines);
                        sw.Close();
                    }
                });
            }
        }
Пример #20
0
        static List <CsvRow> LoadCsv(string inputFile)
        {
            List <CsvRow> inputCsvRows = new List <CsvRow>();

            var csvConfig = new CsvHelper.Configuration.Configuration()
            {
                AllowComments         = false,
                HasHeaderRecord       = true,
                IgnoreBlankLines      = true,
                IncludePrivateMembers = true,
                TrimOptions           = CsvHelper.Configuration.TrimOptions.None,
            };

            using (FileStream inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (TextReader inputReader = new StreamReader(inputStream, Encoding.UTF8))
                    using (CsvReader csvReader = new CsvReader(inputReader, csvConfig))
                    {
                        csvReader.Read();
                        csvReader.ReadHeader();
                        while (csvReader.Read())
                        {
                            StreamingService service     = csvReader.GetField <StreamingService>("Service");
                            string           animeName   = csvReader.GetField <string>("Anime");
                            string           url         = csvReader.GetField <string>("URL");
                            string           malIdString = csvReader.GetField <string>("MAL ID (or n/a)");

                            MalId malId;

                            // could be blank
                            if (string.IsNullOrWhiteSpace(malIdString))
                            {
                                malId = new MalId(malAnimeId: null, specified: false);
                            }
                            else if (malIdString.Equals("n/a", StringComparison.OrdinalIgnoreCase))
                            {
                                // n/a means the stream does not correspond to a MAL anime. Maybe it's a stream of anime reviews
                                // or maybe it's something incredibly obscure.
                                malId = new MalId(malAnimeId: null, specified: true);
                            }
                            else
                            {
                                int malIdInt = int.Parse(malIdString);
                                malId = new MalId(malAnimeId: malIdInt, specified: true);
                            }

                            CsvRow row = new CsvRow(service, animeName, url, malId);
                            inputCsvRows.Add(row);
                        }
                    }

            return(inputCsvRows);
        }
Пример #21
0
        /// <summary>
        /// Used to write to a CSV-formated stream via a <see cref="System.IO.StreamReader" />
        /// </summary>
        /// <param name="stream">The CSV-formated stream to write to</param>
        /// <param name="csvConfiguration">A <see cref="CsvHelper.Configuration" /> configuration specification for how to read the csv file</param>
        public CsvRecordWriter(Stream stream, CsvHelper.Configuration.Configuration csvConfiguration = null)
        {
            var csvConfig = csvConfiguration ?? TextWranglerConfig.DefaultCsvConfigurationFactory();

            if (stream == null || !stream.CanWrite)
            {
                throw new ArgumentNullException(nameof(stream), "Stream must be included and writable");
            }

            // Keep a local ref to the stream reader so we can properly dispose of it on failure in creating the csv writer
            _streamWriter = new StreamWriter(stream);
            _csvWriter    = new CsvWriter(_streamWriter, csvConfig);
        }
Пример #22
0
        public override void Denormalize()
        {
            var conf = new CsvHelper.Configuration.Configuration()
            {
                HasHeaderRecord = false
            };
            using (var streamReader = new StreamReader(Path, Encoding.GetEncoding("SHIFT_JIS")))
            using (var csv = new CsvHelper.CsvReader(streamReader, conf))
            {
                csv.Configuration.RegisterClassMap<SourceMapper>();
                csv.Configuration.ReadingExceptionOccurred = ex =>
                {
                    // Do something instead of throwing an exception.
                    return true;
                };
                var source = csv.GetRecords<Source>().ToList();
                DataTable products = FetchProducts(source.Select(r => r.JanCode).Distinct());

                Func<Source, DataRow, Result> resultSelector = (a, b) =>
                {
                    return new Result()
                    {
                        DeliveredAt = a.DeliveredAt,
                        SupplyChainManagementCode = a.SupplyChainManagementCode,
                        SupplierCode = b["supplier_code"].ToString(),
                        VarietyCode = a.VarietyCode,
                        ModelNo = b["model_no"].ToString(),
                        ProductName = b["product_name"].ToString(),
                        JanCode = a.JanCode,
                        StoreCode = a.StoreCode,
                        StoreName = a.StoreName,
                        Qty = a.Qty,
                        UnitCost = a.UnitCost,
                        Cost = a.Cost,
                        UnitPrice = a.UnitPrice,
                        Price = a.Price,
                    };
                };
                var cmp = EqualityComparer<string>.Default;
                var alookup = source.ToLookup((x) => x.JanCode, cmp);
                var blookup = products.AsEnumerable().ToLookup((x) => x["jan"].ToString(), cmp);

                var keys = new HashSet<string>(alookup.Select(p => p.Key), cmp);
                var query = from key in keys
                            from xa in alookup[key].DefaultIfEmpty(null)
                            from xb in blookup[key].DefaultIfEmpty(null)
                            select resultSelector(xa, xb);
                DenormalizedSchema_ = query.ToList();
            }
        }
Пример #23
0
        public List <DTO.DataDTO> LoadFile(string path)
        {
            StreamReader reader = new StreamReader(path, Encoding.Default);
            var          config = new CsvHelper.Configuration.Configuration
            {
                HasHeaderRecord = false,
                Delimiter       = ";"
            };

            using (CsvReader csvReader = new CsvReader(reader, config, true))
            {
                return(csvReader.GetRecords <DTO.DataDTO>().ToList());
            }
        }
Пример #24
0
        public CsvHelper.Configuration.Configuration ConvertConfiguration()
        {
            // See https://joshclose.github.io/CsvHelper/configuration

            var configuration = new CsvHelper.Configuration.Configuration
            {
                SanitizeForInjection = true,
                HasHeaderRecord      = Configuration.HasHeaders,
                Delimiter            = Configuration.Delimiter,
                Quote   = Configuration.Quote,
                Comment = Configuration.Comment,
            };

            return(configuration);
        }
Пример #25
0
        }     // end sub

        /// <summary>
        /// <para xml:lang="en">Output the names of properties of a object as header of a TSV file.</para>
        /// <para xml:lang="ja">TSVファイルヘッダとして、オブジェクトのプロパティの名前を出力します。</para>
        /// </summary>
        /// <typeparam name="T">
        /// <para xml:lang="en">The type of object to dump.</para>
        /// <para xml:lang="ja">ダンプするオブジェクトの型</para>
        /// </typeparam>
        /// <param name="target">
        /// <para xml:lang="en">
        /// The object whose properties names are read. However, those properties values are not outputted.
        /// Use DumpTsvRecord method, DumpTsv method or other methods to output values.
        /// </para>
        /// <para xml:lang="ja">
        /// プロパティの名前を読み取るオブジェクト。ただしプロパティの値は出力されません。
        /// 値を出力するには DumpTsvRecord メソッドやDumpTsvメソッドなどを使用します。
        /// </para>
        /// </param>
        /// <param name="name">
        /// <para xml:lang="en">The name which is used for a file.</para>
        /// <para xml:lang="ja">ファイルに使用される名前。</para>
        /// </param>
        /// <param name="format">
        /// <para xml:lang="en">
        /// The lambda expression which is used for choosing properties to dump.
        /// Set like <code>a=&gt;new {a.A, a.B}</code>  to dump A property and B property of the object.
        /// Set like <code>a=&gt;a</code>  to dump all properties of the object.
        /// </para>
        /// <para xml:lang="ja">
        /// ダンプするプロパティを選択するラムダ式。
        /// オブジェクトのAプロパティとBプロパティをダンプする場合には、<code>a=&gt;new {a.A, a.B}</code> のように指定します。
        /// 全てのプロパティをダンプする場合には<code>a=&gt;a</code>を指定します。
        /// </para>
        /// </param>
        public static void DumpTsvHeader <T>(T target, string name, Func <T, object> format)
        {
            Dumper.WriteTextFile(name, ".tsv", writer =>
            {
                var configuration = new CsvHelper.Configuration.Configuration {
                    Delimiter = "\t"
                };
                using (var tsv = new CsvHelper.CsvWriter(writer, configuration))
                {
                    var v = format(target);
                    tsv.WriteHeader(v.GetType());
                    tsv.NextRecord();
                } // end using (tsv)
            });
        }         // end sub
Пример #26
0
        public static void WriteToCSV(this MathNet.Numerics.LinearAlgebra.Vector <double> vec, string path)
        {
            CsvHelper.Configuration.Configuration cfg =
                new CsvHelper.Configuration.Configuration(CultureInfo.InvariantCulture);

            using (StreamWriter sw = new StreamWriter(path))
                using (CsvWriter cw = new CsvWriter(sw, cfg))
                {
                    for (int e = 0; e < vec.Count; e++)
                    {
                        cw.WriteField(e);
                        cw.WriteField(vec[e]);
                        cw.NextRecord();
                    }
                }
        }
Пример #27
0
        public void DelimiterReaderWithConfigurationTest()
        {
            var configuration = new CsvHelper.Configuration.Configuration
            {
                Delimiter = ":"
            };

            using (var reader = new StringReader("Id§Name\r\n1§one\r\n"))
                using (var csv = new CsvReader(reader, configuration))
                {
                    var records = csv.GetRecords <DelimiterTestClass>().ToList();
                    var actual  = csv.Configuration.Delimiter;

                    Assert.AreEqual("§", actual);
                }
        }
Пример #28
0
        public async Task <IActionResult> UploadWS(IFormFile file, string LoginId, string SessionKey)
        {
            var(result, userStatus) = IsInvalidSession(LoginId, SessionKey);
            if (!result)
            {
                return(View("~/Views/Users/Login.cshtml"));
            }

            if (file.Length < 1)
            {
                return(View("upload", userStatus));
            }

            try {
                var config = new CsvHelper.Configuration.Configuration
                {
                    HasHeaderRecord   = false,
                    MissingFieldFound = null,
                    IgnoreBlankLines  = true,
                };

                using (var streamReader = new StreamReader(file.OpenReadStream()))
                    using (var csv = new CsvReader(streamReader, config))
                    {
                        IEnumerable <WSCsvRow> wsCsvRow = csv.GetRecords <WSCsvRow>();

                        List <WorkScheduleMaster> wsmList;

                        if (csvValidationWsErr(wsCsvRow, out wsmList))
                        {
                            return(View("upload", userStatus));
                        }

                        await _wsmService.UploadAsync(wsmList);

                        ViewBag.Message = String.Format(ApiConstant.INFO_UPLOAD_WS_01, wsmList.Count);
                    }
            }
            catch (Exception)
            {
                ViewBag.Message = String.Format(ApiConstant.ERR90);
            }


            return(View("upload", userStatus));
        }
Пример #29
0
        private static List<dynamic> GetRecords( string filePath )
        {
            if ( File.Exists( filePath ) )
            {
                using ( var s = File.OpenText( filePath ) )
                {
                    var config = new CsvHelper.Configuration.Configuration();
                    config.BadDataFound = null;
                    var reader = new CsvReader( s, config );
                    reader.Configuration.HasHeaderRecord = true;
                    var records = reader.GetRecords<dynamic>().ToList();
                    return records;
                }
            }

            return null;
        }
Пример #30
0
        static void Main(string[] args)
        {
            CommandLineArgs commandLine = new CommandLineArgs(args);

            List <mal_anime_prerequisite> prereqs = new List <mal_anime_prerequisite>();

            // open input file and parse it

            var csvConfig = new CsvHelper.Configuration.Configuration()
            {
                AllowComments         = false,
                HasHeaderRecord       = true,
                IgnoreBlankLines      = true,
                IncludePrivateMembers = true,
                TrimOptions           = CsvHelper.Configuration.TrimOptions.None,
            };

            using (FileStream inputStream = new FileStream(commandLine.InputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (TextReader inputReader = new StreamReader(inputStream, Encoding.UTF8))
                    using (CsvReader csvReader = new CsvReader(inputReader, csvConfig))
                    {
                        while (csvReader.Read())
                        {
                            string prereqMalUrl = csvReader.GetField <string>("Prerequisite");
                            string animeMalUrl  = csvReader.GetField <string>("Anime");

                            int prereqId = GetMalIdFromMalUrl(prereqMalUrl);
                            int animeId  = GetMalIdFromMalUrl(animeMalUrl);

                            mal_anime_prerequisite prereq = new mal_anime_prerequisite(
                                _mal_anime_id: animeId,
                                _prerequisite_mal_anime_id: prereqId
                                );

                            prereqs.Add(prereq);
                        }
                    }

            string sql = mal_anime_prerequisite.CreateRefreshPrerequisiteMapSql(prereqs);

            using (FileStream outputStream = new FileStream(commandLine.OutputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                using (StreamWriter output = new StreamWriter(outputStream, Encoding.UTF8))
                {
                    output.Write(sql);
                }
        }