Exemplo n.º 1
0
        private void btnFileOpen_Click(object sender, EventArgs e)
        {
            openFile = new OpenFileDialog();
            openFile.InitialDirectory = "c:\\";
            openFile.Filter           = "csv files (*.csv)|*.csv";
            openFile.FilterIndex      = 2;
            openFile.RestoreDirectory = true;

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (FileStream fStream = File.Open(openFile.FileName, FileMode.OpenOrCreate))
                    {
                        var csv = new CsvHelper.CsvHelper(fStream);
                        csv.Configuration.Delimiter           = csvDelimiter.ToCharArray(0, 1)[0];
                        csv.Configuration.UseInvariantCulture = true;
                        tbFilePath.Text = openFile.FileName;
                        tradesList      = csv.Reader.GetRecords <Trade>().ToList();
                        if (tradesList != null && tradesList.Count > 0)
                        {
                            btnImport.Enabled   = true;
                            lblTradesCount.Text = tradesList.Count.ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    tbFilePath.Text = string.Empty;
                    MessageBox.Show("Error: Could not read file from disk. The file could be malformed or the delimiter is not properly defined. Original error: " + ex.Message);
                }
            }
        }
Exemplo n.º 2
0
        public IEnumerable <T> Parse <T>(string fileName) where T : class, ISupplier
        {
            var csv = new CsvHelper.CsvHelper(fileSystem.File.Open(fileName, FileMode.OpenOrCreate));

            var list = csv.Reader.GetRecords <T>();

            return(list);
        }
Exemplo n.º 3
0
 private static IEnumerable<Models.PostCode> GetPostCodes()
 {
     IEnumerable<Models.PostCode> postCodes;
     using (var fileStream = File.Open("Test.csv", FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite))
     {
         var csv = new CsvHelper.CsvHelper(fileStream);
         csv.Configuration.ClassMapping<ReadPostCodeMap, Models.PostCode>();
         postCodes = csv.Reader.GetRecords<Models.PostCode>().ToList();
     }
     return postCodes;
 }
        public List <Instrument> loadInstruments()
        {
            List <Instrument> instruments;

            try
            {
                using (FileStream fStream = File.Open("instrument-list.txt", FileMode.OpenOrCreate))
                {
                    var csv = new CsvHelper.CsvHelper(fStream);
                    csv.Configuration.Delimiter           = ';';
                    csv.Configuration.UseInvariantCulture = true;
                    instruments = csv.Reader.GetRecords <Instrument>().ToList();
                }
                return(instruments);
            }
            catch (Exception e)
            {
                throw new CannotLoadInstrumentsException(string.Format("There is some problem starting application. Cannot load the instrumet list: {0}", e.Message));
            }
        }
Exemplo n.º 5
0
        public static ParseResult ParseFromCsv(string fileName)
        {
            var result = new ParseResult();

            try
            {
                var csv = new CsvHelper.CsvHelper(File.OpenRead(fileName));
                var records = csv.Reader.GetRecords<IngCsvRecord>();
                result.Records.AddRange(records);
            }
            catch(FileNotFoundException)
            {
                result.Error = string.Format(Errors.IncorrectFile, fileName);
            }
            catch (Exception e)
            {
                result.Error = string.Format(Errors.InvalidFile, fileName, e.Message);
            }

            return result;
        }
Exemplo n.º 6
0
        public static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine(Errors.IncorrectArguments);
            }
            else
            {
                var result = IngCsvParser.ParseFromCsv(args[0]);
                if (!string.IsNullOrEmpty(result.Error))
                {
                    Console.Error.WriteLine(result.Error);
                }
                else if (result.Records.Count == 0)
                {
                    Console.Error.WriteLine(Errors.NoRecords);
                }
                else
                {
                    try
                    {
                        var fullyParsed = IngCsvParser.ParseOutTransactions(result.Records);
                        var csv = new CsvHelper.CsvHelper(File.OpenWrite("out.csv"));
                        csv.Writer.WriteRecords(fullyParsed);
                        Console.WriteLine("Success!");
                    }
                    catch(Exception e)
                    {
                        Console.Error.WriteLine(e.ToString());
                    }
                }
            }

            Console.WriteLine("Press any key to exit...");
            Console.Read();
        }
Exemplo n.º 7
0
 private static void WriteCsv(IEnumerable<Models.PostCode> postCodes)
 {
     var csvFile = Guid.NewGuid() + ".csv";
     using (var f = File.Create(csvFile))
     {
         var csv = new CsvHelper.CsvHelper(f);
         csv.Configuration.ClassMapping<WritePostCodeMap, Models.PostCode>();
         csv.Writer.WriteRecords(postCodes);
     }
 }