示例#1
0
        protected void AssertRead <T>(string testInput, CsvFileDescription fileDescription, IEnumerable <T> expected)
            where T : class, IAssertable <T>, new()
        {
            List <T> actual = CsvContext.ReadString <T>(testInput, fileDescription).ToList();

            AssertCollectionsEqual <T>(actual, expected);
        }
        public void TestInitialize()
        {
            _description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns    = true,
            };
            _input =
                @"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington";

            byte[] stringAsByteArray = Encoding.UTF8.GetBytes(_input);
            Stream stream            = new MemoryStream(stringAsByteArray);

            _sr         = new StreamReader(stream, Encoding.UTF8);
            _dataAccess = new FileDataAccess(_sr, _description);

            _cs = new CsvStream(_sr, null, _description.SeparatorChar,
                                _description.IgnoreTrailingSeparatorChar);
            _row    = new DataRow();
            _fm     = new FieldMapperReading <Person>(_description, null, false);
            _ae     = new AggregatedException(typeof(Person).ToString(), null, _description.MaximumNbrExceptions);
            _reader = new RowReader <Person>(_description, _ae);
        }
示例#3
0
        /// <summary>
        /// Executes a Read and tests whether it outputs the expected records.
        /// </summary>
        /// <typeparam name="T">
        /// Type of the output elements.
        /// </typeparam>
        /// <param name="testInput">
        /// String representing the contents of the file or StreamReader. This string is fed to the Read method
        /// as though it came from a file or StreamReader.
        /// </param>
        /// <param name="fileDescription">
        /// Passed to Read.
        /// </param>
        /// <param name="expected">
        /// Expected output.
        /// </param>
        public void AssertRead <T>(string testInput, CsvFileDescription fileDescription, IEnumerable <T> expected)
            where T : class, IAssertable <T>, new()
        {
            IEnumerable <T> actual = TestRead <T>(testInput, fileDescription);

            AssertCollectionsEqual <T>(actual, expected);
        }
示例#4
0
        public static IEnumerable <T> Read <T>(StreamReader stream, CsvFileDescription fileDescription)
            where T : class, new()
        {
            FileDataAccess dataAccess = new FileDataAccess(stream, fileDescription);

            return(dataAccess.ReadData <T>(null));
        }
示例#5
0
 public FieldMapperReading(
     CsvFileDescription fileDescription,
     string fileName,
     bool writingFile)
     : base(fileDescription, fileName, writingFile)
 {
 }
        public void ExportToCSV()
        {
            CsvContext         cc = new CsvContext();
            CsvFileDescription outputFileDescription = new CsvFileDescription
            {
                QuoteAllFields          = false,
                SeparatorChar           = ',', // tab delimited
                FirstLineHasColumnNames = true,
                FileCultureName         = "en-US"
            };

            // prepare invoices
            var exportCustomers =
                from cs in View.CustomerListData
                select new
            {
                Kode       = cs.Code,
                Perusahaan = cs.CompanyName,
                Alamat     = cs.Address,
                Kota       = cs.City.Name,
                Telepon    = cs.PhoneNumber,
                NamaKontak = cs.ContactPerson
            };

            cc.Write(exportCustomers, View.ExportFileName, outputFileDescription);
        }
示例#7
0
        public void GoodFileNoSeparatorCharUSEnglish()
        {
            // Arrange

            CsvFileDescription fileDescription_namesUs = new CsvFileDescription
            {
                NoSeparatorChar = true,
                UseOutputFormatForParsingCsvValue = false,
                FirstLineHasColumnNames           = false,
                EnforceCsvColumnAttribute         = true, // default is false
                FileCultureName = "en-US"                 // default is the current culture
            };

            string testInput =
                @"AAAAAAAA34.18405/23/08\n
BBBBBBBB10.31105/12/12\n
CCCCCCCC12.00012/23/08";

            var expected = new[] {
                new ProductDataCharLength()
                {
                    name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
                },
                new ProductDataCharLength {
                    name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
                },
                new ProductDataCharLength {
                    name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
                }
            };

            // Act and Assert

            AssertRead(testInput, fileDescription_namesUs, expected);
        }
示例#8
0
        public List <ManagerReport> getReportList(string path)
        {
            //string[] str = System.IO.Directory.GetFiles(path, "*.csv");

            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar             = ',',
                FirstLineHasColumnNames   = false,
                EnforceCsvColumnAttribute = true,
            };

            CsvContext cc = new CsvContext();

            List <ManagerReport> rep = cc.Read <ManagerReport>(path, inputFileDescription).ToList <ManagerReport>();

            string   managerName = Path.GetFileName(path).Split('_').First();
            string   datestr     = Path.GetFileName(path).Split('_').Last();
            DateTime date        = new DateTime(Int32.Parse(datestr.Substring(4, 4)), Int32.Parse(datestr.Substring(2, 2)), Int32.Parse(datestr.Substring(0, 2)));

            foreach (ManagerReport repos in rep)
            {
                repos.ManagerLastName = managerName;
                repos.ReportDate      = date;
            }

            return(rep);
        }
示例#9
0
 public FileDataAccess(StreamReader stream, CsvFileDescription fileDescription)
 {
     _stream         = stream;
     FileDescription = fileDescription;
     Cs = new CsvStream(stream, null, fileDescription.SeparatorChar,
                        fileDescription.IgnoreTrailingSeparatorChar);
 }
        public void ExportToCSV()
        {
            CsvContext         cc = new CsvContext();
            CsvFileDescription outputFileDescription = new CsvFileDescription
            {
                QuoteAllFields          = false,
                SeparatorChar           = ',', // tab delimited
                FirstLineHasColumnNames = true,
                FileCultureName         = "en-US"
            };

            // prepare invoices
            var exportSpareparts =
                from sp in View.SparepartListData
                select new
            {
                Tanggal  = sp.SPK.CreateDate.ToString("yyyyMMdd"),
                Nopol    = sp.SPK.Vehicle.ActiveLicenseNumber,
                Kode     = sp.Sparepart.Code,
                Nama     = sp.Sparepart.Name,
                Unit     = sp.Sparepart.UnitReference.Name,
                Qty      = sp.TotalQuantity,
                SubTotal = sp.TotalPrice,
                Kategori = sp.Category
            };

            cc.Write(exportSpareparts, View.ExportFileName, outputFileDescription);
        }
        public IActionResult DatabaseUpload(IFormFile?files)
        {
            using (_context) using (var transaction = _context.Database.BeginTransaction())
                {
                    if (files != null)
                    {
                        CsvFileDescription csvFileDescription = new CsvFileDescription
                        {
                            SeparatorChar           = ',',
                            FirstLineHasColumnNames = true
                        };
                        LINQtoCSV.CsvContext csvContext   = new LINQtoCSV.CsvContext();
                        StreamReader         streamReader = new StreamReader(files.OpenReadStream());
                        IEnumerable <Member> list         = csvContext.Read <Member>(streamReader, csvFileDescription);

                        //_context.Member.UpdateRange(list);
                        _context.Member.AddRange(list);
                        _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.TestMembers ON");
                        _context.SaveChanges();
                        _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.TestMembers OFF");
                        transaction.Commit();
                    }
                }
            return(Redirect(nameof(Database)));
        }
        public void TestInitialize()
        {
            _description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns    = true,
            };
            _input =
                @"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington
2,Jane,Doe,20,New York
";
            Expected = new[]
            {
                new Person
                {
                    Name     = "John",
                    LastName = "Doe",
                    Age      = 15
                },
                new Person
                {
                    Name     = "Jane",
                    LastName = "Doe",
                    Age      = 20
                }
            };
            _stream         = _input.GetStreamReader();
            _dataAccess     = new FileDataAccess(_stream, _description);
            _reader         = _dataAccess.ReadDataPreparation <Person>(null);
            _dataAccess.Row = new DataRow();
        }
        }//end Main method

        /// <summary>
        /// Loading the csv dataset file into DatasetItems
        /// </summary>
        /// <param name="csvFile"></param>
        /// <returns></returns>
        public static IEnumerable <DatasetItem> ReadFromCsv(string csvFile)
        {
            IEnumerable <DatasetItem> datasetItemList = null;

            try
            {
                //Read From Csv
                var csvFileDescription = new CsvFileDescription
                {
                    SeparatorChar             = ',',     //Specify the separator character.
                    FirstLineHasColumnNames   = false,
                    FileCultureName           = "en-US", // default is the current culture
                    EnforceCsvColumnAttribute = true
                };

                var csvContext = new CsvContext();

                datasetItemList = csvContext.Read <DatasetItem>(csvFile, csvFileDescription);
            }
            catch (AggregatedException ae)
            {
                List <Exception> innerExceptionsList = (List <Exception>)ae.Data["InnerExceptionList"];

                foreach (Exception e in innerExceptionsList)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return(datasetItemList);
        }
示例#14
0
        private string TestWrite <T>(IEnumerable <T> values, CsvFileDescription fileDescription) where T : class
        {
            TextWriter stream = new StringWriter();

            CsvContext.Write(values, stream, fileDescription);
            return(stream.ToString());
        }
        public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUSEnglish()
        {
            // Arrange

            CsvFileDescription fileDescription_namesUs = new CsvFileDescription
            {
                SeparatorChar               = ',',
                IgnoreUnknownColumns        = true,
                UseFieldIndexForReadingData = true,
                FirstLineHasColumnNames     = false,
                EnforceCsvColumnAttribute   = true,   // default is false
                FileCultureName             = "en-US" // default is the current culture
            };

            string testInput =
                "AAAAAAAA,__,34.184,05/23/08" + Environment.NewLine +
                "BBBBBBBB,__,10.311,05/12/12" + Environment.NewLine +
                "CCCCCCCC,__,12.000,12/23/08";

            var expected = new[] {
                new ProductDataSpecificFieldIndex
                {
                    Name = "AAAAAAAA", Weight = 34.184, StartDate = new DateTime(2008, 5, 23),
                },
                new ProductDataSpecificFieldIndex {
                    Name = "BBBBBBBB", Weight = 10.311, StartDate = new DateTime(2012, 5, 12),
                },
                new ProductDataSpecificFieldIndex {
                    Name = "CCCCCCCC", Weight = 12.000, StartDate = new DateTime(2008, 12, 23),
                }
            };

            AssertRead(testInput, fileDescription_namesUs, expected);
        }
示例#16
0
        public void RegexColumnWithInvalidDataShouldThrowException()
        {
            var description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns    = true,
            };

            //The following input has 5 columns: Id | Name | Last Name | Age | City. Only the Name, Last Name and Age will be read.

            var input =
                @"Id,Name,Last Name,Age,Gender,City
1,John,Doe,15,Test,Washington
2,Jane,Doe,20,Test,New York
";
            var expected = new[]
            {
                new PersonRegex
                {
                    Name     = "John",
                    LastName = "Doe",
                    Age      = 15
                },
                new PersonRegex
                {
                    Name     = "Jane",
                    LastName = "Doe",
                    Age      = 20
                },
            };

            AssertRead(input, description, expected);
        }
        public void ExportToCSV()
        {
            CsvContext         cc = new CsvContext();
            CsvFileDescription outputFileDescription = new CsvFileDescription
            {
                QuoteAllFields          = false,
                SeparatorChar           = ',', // tab delimited
                FirstLineHasColumnNames = true,
                FileCultureName         = "en-US"
            };

            // prepare invoices
            var exportGuestBooks =
                from gb in View.GuestBookListData
                select new
            {
                Nopol           = gb.Vehicle.ActiveLicenseNumber,
                Customer        = gb.Vehicle.Customer.CompanyName,
                Merek           = gb.Vehicle.Brand.Name,
                Tipe            = gb.Vehicle.Type.Name,
                WaktuKedatangan = gb.ArrivalTime,
                Keterangan      = gb.Description
            };

            cc.Write(exportGuestBooks, View.ExportFileName, outputFileDescription);
        }
示例#18
0
        public void FileWithUnknownColumns_ShouldDiscardColumns()
        {
            var description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns    = true,
            };

            //The following input has 5 columns: Id | Name | Last Name | Age | City. Only the Name, Last Name and Age will be read.

            string input =
                @"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington
2,Jane,Doe,20,New York
";
            var expected = new[]
            {
                new Person
                {
                    Name     = "John",
                    LastName = "Doe",
                    Age      = 15
                },
                new Person
                {
                    Name     = "Jane",
                    LastName = "Doe",
                    Age      = 20
                },
            };

            AssertRead(input, description, expected);
        }
示例#19
0
        public void GoodFileUsingOutputFormatForParsingDatesCharUSEnglish()
        {
            // Arrange

            CsvFileDescription fileDescription_namesUs = new CsvFileDescription
            {
                SeparatorChar                     = ';',
                FirstLineHasColumnNames           = false,
                UseOutputFormatForParsingCsvValue = true,
                EnforceCsvColumnAttribute         = true,   // default is false
                FileCultureName                   = "en-US" // default is the current culture
            };

            string testInput =
                "AAAAAAAA;052308" + Environment.NewLine +
                "BBBBBBBB;051212" + Environment.NewLine +
                "CCCCCCCC;122308";

            var expected = new[] {
                new ProductDataParsingOutputFormat()
                {
                    name = "AAAAAAAA", startDate = new DateTime(2008, 5, 23),
                },
                new ProductDataParsingOutputFormat {
                    name = "BBBBBBBB", startDate = new DateTime(2012, 5, 12),
                },
                new ProductDataParsingOutputFormat {
                    name = "CCCCCCCC", startDate = new DateTime(2008, 12, 23),
                }
            };

            // Act and Assert

            AssertRead(testInput, fileDescription_namesUs, expected);
        }
示例#20
0
        public void GoodFileShouldReadRawData()
        {
            var description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = false
            };

            string input =
                @"1,John,Doe,15,Washington
2,Jane,Doe,20,New York
";
            var expected = new[]
            {
                new TestDataRow
                {
                    new DataRowItem("1", 1),
                    new DataRowItem("John", 1),
                    new DataRowItem("Doe", 1),
                    new DataRowItem("15", 1),
                    new DataRowItem("Washington", 1)
                },
                new TestDataRow
                {
                    new DataRowItem("2", 2),
                    new DataRowItem("Jane", 2),
                    new DataRowItem("Doe", 2),
                    new DataRowItem("20", 2),
                    new DataRowItem("New York", 2)
                }
            };

            AssertRead(input, description, expected);
        }
 private void LoadData()
 {
     try
     {
         //data courtesy of https://data.gov.in/catalog/company-master-data
         Status = "Loading CSV...";
         var ctx  = new CsvContext();
         var desc = new CsvFileDescription {
             SeparatorChar = ',', IgnoreUnknownColumns = true
         };
         _list = ctx.Read <CompanyInfo>("demodata.csv", desc)
                 .OrderBy(i => i.CompanyName)
                 .ToArray();
         Status        = $"Loaded {_list.Length:N0} entries";
         SelectedEntry = _list.Skip(1000).First();
     }
     catch (AggregatedException ex)
     {
         var sb = new StringBuilder();
         foreach (var x in ex.m_InnerExceptionsList)
         {
             sb.AppendLine($" {x.Message}");
         }
         Status = $"Error loading data. {sb}";
     }
     catch (Exception ex)
     {
         Status = $"Error loading data. {ex.Message}";
     }
 }
        public void ExportToCSV()
        {
            CsvContext         cc = new CsvContext();
            CsvFileDescription outputFileDescription = new CsvFileDescription
            {
                QuoteAllFields          = false,
                SeparatorChar           = ',', // tab delimited
                FirstLineHasColumnNames = true,
                FileCultureName         = "en-US"
            };

            // prepare invoices
            var exportVehicles =
                from ve in View.VehicleListData
                select new
            {
                Nopol          = ve.ActiveLicenseNumber,
                Customer       = ve.Customer.CompanyName,
                Kelompok       = ve.VehicleGroup.Name,
                Merek          = ve.Brand.Name,
                Tipe           = ve.Type.Name,
                TahunPembelian = ve.YearOfPurchase
            };

            cc.Write(exportVehicles, View.ExportFileName, outputFileDescription);
        }
        public Tuple <bool, string> cargarListaFuncionario(string path)
        {
            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar           = ',',  //Indica qué es lo que separa cada valor en el archivo
                FirstLineHasColumnNames = true, //La primera fila corresponde a los títulos de los campos, no a un campo específico
                IgnoreUnknownColumns    = true  // Linea para evitar errores o algunos datos no desead0s
            };
            CsvContext cc = new CsvContext();
            //Este IEnumerable tiene cada modelo que fue llenado con los datos del CSV
            IEnumerable <ListaFuncionario> datos = cc.Read <ListaFuncionario>(path, inputFileDescription);
            List <ListaFuncionario>        lista = datos.ToList();

            //Se valida cada fila de CSV
            ValidadorListaFuncionarios val = new ValidadorListaFuncionarios();
            bool error;
            int  filaActual = 0;

            foreach (ListaFuncionario f in lista)
            {
                ++filaActual;
                error = val.Validar(f, filaActual).Item1;
                if (!error)
                {
                    return(Tuple.Create(false, val.Validar(f, filaActual).Item2));
                }
            }

            foreach (ListaFuncionario f in lista)
            {
                insertarListaFuncionario(f);
            }
            return(Tuple.Create(true, ""));
        }
示例#24
0
        internal static void FromCSV(string path, out PartList parts, out BoardList boards)
        {
            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar               = ',',
                FirstLineHasColumnNames     = false,
                EnforceCsvColumnAttribute   = true,
                UseFieldIndexForReadingData = true
            };
            CsvContext cc = new CsvContext();

            IEnumerable <CSVRecord> records = new List <CSVRecord>(
                cc.Read <CSVRecord>(path, inputFileDescription));

            parts  = new PartList();
            boards = new BoardList();
            foreach (var iline in records.Where(t => !t.ItemType.StartsWith("#")))
            {
                if (iline.ItemType.ToLower() == "board")
                {
                    boards.Append(new BoardNode(iline.PartID, double.Parse(iline.Length), double.Parse(iline.Width)));
                }
                else
                {
                    parts.Append(new PartNode(iline.PartID, double.Parse(iline.Length), double.Parse(iline.Width)));
                }
            }
        }
示例#25
0
 public static void Write <T>(IEnumerable <T> values, string fileName, CsvFileDescription fileDescription)
 {
     using (StreamWriter sw = new StreamWriter(fileName, false, fileDescription.TextEncoding))
     {
         WriteData(values, fileName, sw, fileDescription);
     }
 }
示例#26
0
        public static void FromCutlistPlusCSV(string filePath, out PartList parts, out BoardList boards)
        {
            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar               = ',',
                FirstLineHasColumnNames     = false,
                EnforceCsvColumnAttribute   = true,
                UseFieldIndexForReadingData = true
            };
            CsvContext cc = new CsvContext();

            IEnumerable <CutListPlusCSVRecord> records = new List <CutListPlusCSVRecord>(
                cc.Read <CutListPlusCSVRecord>(filePath, inputFileDescription));

            parts  = new PartList();
            boards = new BoardList();
            foreach (var iline in records.Where(t => t.PartNumber != "Part #"))
            {
                if (iline.MaterialName == "Stock")
                {
                    boards.Append(new BoardNode(iline.PartName, double.Parse(iline.Length.Replace("mm", "")), double.Parse(iline.Width.Replace("mm", ""))));
                }
                else
                {
                    parts.Append(new PartNode(iline.PartName, double.Parse(iline.Length.Replace("mm", "")), double.Parse(iline.Width.Replace("mm", ""))));
                }
            }
        }
        public void ExportToCSV()
        {
            CsvContext         cc = new CsvContext();
            CsvFileDescription outputFileDescription = new CsvFileDescription
            {
                QuoteAllFields          = false,
                SeparatorChar           = ',', // tab delimited
                FirstLineHasColumnNames = true,
                FileCultureName         = "en-US"
            };

            // prepare invoices
            var exportRoles =
                from ro in View.UserRoleListData
                select new
            {
                NamaDepan    = ro.User.FirstName,
                NamaBelakang = ro.User.LastName,
                Username     = ro.User.UserName,
                Role         = ro.Role.Name,
                StatusActive = ro.User.IsActive ? "Aktif" : "Tidak Aktif"
            };

            cc.Write(exportRoles, View.ExportFileName, outputFileDescription);
        }
        public string GetUiDataCsv(SprintProgressVm sprintProgressVm)//TODO convert to property on SprintProgressVm.cs
        {
            var separatorChar = ',';

            var csv = $"{sprintProgressVm.UiDataObject.IterationDetails.Iteration}{separatorChar}{sprintProgressVm.UiDataObject.IterationDetails.Start}{separatorChar}{sprintProgressVm.UiDataObject.IterationDetails.End}{Environment.NewLine}";

            var csvFileDescription = new CsvFileDescription
            {
                SeparatorChar = separatorChar,// '\t' for tab delimited
                UseOutputFormatForParsingCsvValue = true
            };

            using (var memoryStream = new MemoryStream())
            {
                using (TextWriter textWriter = new StreamWriter(memoryStream))
                {
                    var csvContext = new CsvContext();
                    csvContext.Write(sprintProgressVm.UiDataObject.WorkItemData.ToList(), textWriter, csvFileDescription);
                    textWriter.Flush();
                    memoryStream.Position = 0;
                    csv += Encoding.ASCII.GetString(memoryStream.ToArray());
                }
            }

            return(csv);
        }
示例#29
0
        public dynamic GetPackagingDeliveryCharge(string item, int count)
        {
            if (count <= 0)
            {
                return(-1);
            }
            var CSVFile = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true
            };
            int Charge  = 0;
            var CSV     = new CsvContext();
            var Charges = from values in CSV.Read <Item>(@"./Items.csv", CSVFile)
                          where (values.ItemType.Trim().ToUpper() == item.ToUpper())
                          select new
            {
                DeliveryCharge  = values.Delivery,
                PackagingCharge = values.Packaging
            };
            var Fee = Charges.Select(charge => charge.DeliveryCharge + charge.PackagingCharge).ToList();

            foreach (int value in Fee)
            {
                Charge += value;
            }
            return(Charge * count);
        }
示例#30
0
        /// <summary>
        /// Initialize the provider
        /// </summary>
        /// <returns></returns>
        public void Seek(DateTime?startDate = null, DateTime?endDate = null)
        {
            StartDate    = startDate;
            EndDate      = endDate;
            PeriodLoaded = 0;

            CsvFileDescription descriptor = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                FileCultureInfo         = System.Globalization.CultureInfo.InvariantCulture
            };

            CsvContext ctx = new CsvContext();

            Data = ctx.Read <Period>(FilePath, descriptor);

            if (StartDate.HasValue)
            {
                Data = Data.Where(d => d.Date >= StartDate.Value);
            }

            if (EndDate.HasValue)
            {
                Data = Data.Where(d => d.Date < EndDate);
            }

            Data = Data.OrderBy(d => d.Date);
        }