public void FullClassBuilding()
        {
            var cb = new DelimitedClassBuilder("Customers", ",");
            cb.IgnoreFirstLines = 1;
            cb.IgnoreEmptyLines = true;

            cb.AddField("Field1", typeof(DateTime));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("Field2", typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar = '"';

            cb.AddField("Field3", typeof(int));

            mEngine = new FileHelperEngine(cb.CreateRecordClass());

            DataTable dt = mEngine.ReadFileAsDT(TestCommon.GetPath("Good", "Test2.txt"));

            Assert.AreEqual(4, dt.Rows.Count);
            Assert.AreEqual(4, mEngine.TotalRecords);
            Assert.AreEqual(0, mEngine.ErrorManager.ErrorCount);

            Assert.AreEqual("Field1", dt.Columns[0].ColumnName);
            Assert.AreEqual("Field2", dt.Columns[1].ColumnName);
            Assert.AreEqual("Field3", dt.Columns[2].ColumnName);

            Assert.AreEqual("Hola", dt.Rows[0][1]);
            Assert.AreEqual(DateTime.Today, dt.Rows[2][0]);
        }
Exemplo n.º 2
0
        public void RunTimeEmptyGuidProperties()
        {
            var builder = new DelimitedClassBuilder("EntityWithGuid", "\t");
            builder.AddField("Name", typeof(string));
            builder.AddField("Id", typeof(Guid));
            builder.LastField.FieldNullValue = Guid.Empty;

            var engine = new FileHelperEngine(builder.CreateRecordClass())
            {
                Options = { IgnoreFirstLines = 1 }
            };

            const string inputValue = @"Name	Id
            first
            second	";

            var records = engine.ReadString(inputValue);
            records.Length.AssertEqualTo(2);

            dynamic record = records[0];
            ((Guid)record.Id).AssertEqualTo(Guid.Empty);
            ((string)record.Name).AssertEqualTo("first");

            record = records[1];
            ((Guid)record.Id).AssertEqualTo(Guid.Empty);
            ((string)record.Name).AssertEqualTo("second");
        }
		public void TestingNameAndTypes()
        {
            var cb = new DelimitedClassBuilder("Customers", ",");
            cb.IgnoreFirstLines = 1;
            cb.IgnoreEmptyLines = true;

            cb.AddField("Field1", typeof (DateTime));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("Field2", typeof (string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar = '"';

            cb.AddField("Field3", typeof (int));

            mEngine = new FileHelperEngine(cb.CreateRecordClass());

            DataTable dt = mEngine.ReadFileAsDT(TestCommon.GetPath("Good", "Test2.txt"));

            Assert.AreEqual("Field1", dt.Columns[0].ColumnName);
            Assert.AreEqual(typeof (DateTime), dt.Columns[0].DataType);

            Assert.AreEqual("Field2", dt.Columns[1].ColumnName);
            Assert.AreEqual(typeof (string), dt.Columns[1].DataType);

            Assert.AreEqual("Field3", dt.Columns[2].ColumnName);
            Assert.AreEqual(typeof (int), dt.Columns[2].DataType);
        }
 public ReadUsersFromFileDynamic()
 {
     var userRecordClassBuilder = new DelimitedClassBuilder("UserRecord","\t");
     userRecordClassBuilder.IgnoreFirstLines = 1;
     userRecordClassBuilder.AddField("Id", typeof(Int32));
     userRecordClassBuilder.AddField("Name", typeof(String));
     userRecordClassBuilder.AddField("Email", typeof(String));
     _tblClass = userRecordClassBuilder.CreateRecordClass();
 }
Exemplo n.º 5
0
		public void ReadAsDataTable2()
        {
            var cb = new DelimitedClassBuilder("ImportContact", ",");
            cb.IgnoreEmptyLines = true;
            cb.GenerateProperties = true;

            cb.AddField("FirstName", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("LastName", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetNumber", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetAddress", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Unit", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("City", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("State", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Zip", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            var engine = new FileHelperEngine(cb.CreateRecordClass());

            DataTable contactData = engine.ReadFileAsDT(TestCommon.GetPath("Good", "ReadAsDataTable.txt"));

            Assert.AreEqual(3, contactData.Rows.Count);
            Assert.AreEqual(8, contactData.Columns.Count);

            Assert.AreEqual("Alex & Jen", contactData.Rows[0][0].ToString());
            Assert.AreEqual("Mark & Lisa K", contactData.Rows[1][0].ToString());

            // new DelimitedClassBuilder("", ",");
        }
Exemplo n.º 6
0
		public void ReadAsDataTable1()
        {
            var cb = new DelimitedClassBuilder("ImportContact", ",");
            cb.IgnoreEmptyLines = true;
            cb.GenerateProperties = true;

            cb.AddField("FirstName", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("LastName", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetNumber", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetAddress", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Unit", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("City", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("State", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Zip", typeof (string));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            var engine = new FileHelperEngine(cb.CreateRecordClass());

            string source = "Alex & Jen,Bouquet,1815,Bell Rd,, Batavia,OH,45103" + Environment.NewLine +
                            "Mark & Lisa K ,Arlinghaus,1817,Bell Rd,, Batavia,OH,45103" + Environment.NewLine +
                            "Ed & Karen S ,Craycraft,1819,Bell Rd,, Batavia,OH,45103" + Environment.NewLine;

            DataTable contactData = engine.ReadStringAsDT(source);

            Assert.AreEqual(3, contactData.Rows.Count);
            Assert.AreEqual(8, contactData.Columns.Count);

            Assert.AreEqual("Alex & Jen", contactData.Rows[0][0].ToString());
            Assert.AreEqual("Mark & Lisa K", contactData.Rows[1][0].ToString());

            // new DelimitedClassBuilder("", ",");
        }
Exemplo n.º 7
0
        public void RunTimeMultimpleInstances()
        {
            var cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.AddField("Field1", "int?");
            cb.AddField("Field2", typeof(int?));
            cb.AddField("Field3", "Nullable<int>");
            cb.AddField("Field4", typeof(Nullable<int>));

            cb.CreateRecordClass();
            cb.CreateRecordClass();
        }
        private void CreateDelimiterOptions(string[][] sampleData, List<RecordFormatInfo> res, char delimiter)
        {
            List<DelimiterInfo> delimiters = new List<DelimiterInfo>();

            if (delimiter == '\0')
                delimiters = GetDelimiters(sampleData);
            else
                delimiters.Add(GetDelimiterInfo(sampleData, delimiter));

            foreach (DelimiterInfo info in delimiters)
            {
                RecordFormatInfo format = new RecordFormatInfo();
                format.mConfidence = (int)((1 - info.Deviation ) * 100);
                AdjustConfidence(format, info);

                DelimitedClassBuilder builder = new DelimitedClassBuilder("AutoDetectedClass", info.Delimiter.ToString());
                builder.IgnoreFirstLines = FileHasHeaders ? 1 : 0;
                var firstLineSplitted = sampleData[0][0].Split(info.Delimiter);
                for (int i = 0; i < info.Max + 1; i++)
                {
                    string name = "Field " + (i + 1).ToString().PadLeft(3, '0');
                    if (FileHasHeaders && i < firstLineSplitted.Length)
                        name = firstLineSplitted[i];

                    var f = builder.AddField(StringHelper.ToValidIdentifier(name));
                    if (i > info.Min)
                        f.FieldOptional = true;
                }

                format.mClassBuilder = builder;

                res.Add(format);
            }
        }
Exemplo n.º 9
0
        public void RunTimeNullableFields()
        {
            DelimitedClassBuilder cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.AddField("Field1", "int?");
            cb.AddField("Field2", typeof(int?));
            cb.AddField("Field3", "Nullable<int>");
            cb.AddField("Field4", typeof (Nullable<int>));
        }
		public void SaveLoadXmlFileDelimited2()
        {
            var cb = new DelimitedClassBuilder("Customers", ",");
            cb.IgnoreFirstLines = 1;
            cb.IgnoreEmptyLines = true;

            cb.AddField("Field1", typeof(DateTime));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("FieldTwo", typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar = '"';

            cb.AddField("Field333", typeof(int));

            cb.SaveToXml(@"dynamic.xml");

            mEngine = new FileHelperEngine(ClassBuilder.ClassFromXmlFile("dynamic.xml"));

            Assert.AreEqual("Customers", mEngine.RecordType.Name);
            Assert.AreEqual(3, mEngine.RecordType.GetFields().Length);
            Assert.AreEqual("Field1", mEngine.RecordType.GetFields()[0].Name);
        }
        public void SaveLoadXmlFileDelimited()
        {
            var cb = new DelimitedClassBuilder("Customers", ",");
            cb.IgnoreFirstLines = 1;
            cb.IgnoreEmptyLines = true;

            cb.AddField("Field1", typeof(DateTime));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("FieldTwo", typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar = '"';

            cb.AddField("Field333", typeof(int));

            cb.SaveToXml(@"dynamic.xml");

            var loaded = (DelimitedClassBuilder)ClassBuilder.LoadFromXml(@"dynamic.xml");

            Assert.AreEqual("Field1", loaded.FieldByIndex(0).FieldName);
            Assert.AreEqual("FieldTwo", loaded.FieldByIndex(1).FieldName);
            Assert.AreEqual("Field333", loaded.FieldByIndex(2).FieldName);

            Assert.AreEqual("System.DateTime", loaded.FieldByIndex(0).FieldType);
            Assert.AreEqual("System.String", loaded.FieldByIndex(1).FieldType);
            Assert.AreEqual("System.Int32", loaded.FieldByIndex(2).FieldType);

            Assert.AreEqual(QuoteMode.AlwaysQuoted, loaded.FieldByIndex(0).QuoteMode);
            Assert.AreEqual(false, loaded.FieldByIndex(0).FieldQuoted);

            Assert.AreEqual('"', loaded.FieldByIndex(1).QuoteChar);
            Assert.AreEqual(true, loaded.FieldByIndex(1).FieldQuoted);
        }
		public void LoopingFields()
        {
            var cb = new DelimitedClassBuilder("MyClass", ",");

            string[] lst = { "fieldOne", "fieldTwo", "fieldThree" };

            for (int i = 0; i < lst.Length; i++)
                cb.AddField(lst[i], typeof(string));

            mEngine = new FileHelperEngine(cb.CreateRecordClass());
        }
		public void ReadAsDataTableWithCustomConverter()
        {
            var fields = new[] {
                "FirstName",
                "LastName",
                "StreetNumber",
                "StreetAddress",
                "Unit",
                "City",
                "State",
            };
            var cb = new DelimitedClassBuilder("ImportContact", ",");
            // Add assembly reference
            cb.AdditionalReferences.Add(typeof(MyCustomConverter).Assembly);

            foreach (var f in fields)
            {
                cb.AddField(f, typeof(string));
                cb.LastField.TrimMode = TrimMode.Both;
                cb.LastField.FieldQuoted = false;
            }

            cb.AddField("Zip", typeof(int?));
            cb.LastField.Converter.TypeName = "FileHelpers.Tests.Dynamic.DelimitedClassBuilderTests.MyCustomConverter";

            mEngine = new FileHelperEngine(cb.CreateRecordClass());

            string source = "Alex & Jen,Bouquet,1815,Bell Rd,, Batavia,OH,45103" + Environment.NewLine +
                            "Mark & Lisa K ,Arlinghaus,1817,Bell Rd,, Batavia,OH,NaN" + Environment.NewLine +
                            "Ed & Karen S ,Craycraft,1819,Bell Rd,, Batavia,OH,45103" + Environment.NewLine;

            var contactData = mEngine.ReadString(source);

            Assert.AreEqual(3, contactData.Length);
            var zip = mEngine.RecordType.GetFields()[7];
            Assert.AreEqual("Zip", zip.Name);
            Assert.IsNull(zip.GetValue(contactData[1]));
            Assert.AreEqual((decimal)45103, zip.GetValue(contactData[2]));
        }
        public void SaveLoadXmlOptionsString()
        {
            var cbOrig = new DelimitedClassBuilder("Customers", ",");
            cbOrig.AddField("Field1", typeof(DateTime));
            cbOrig.AddField("FieldTwo", typeof(string));

            cbOrig.RecordCondition.Condition = RecordCondition.ExcludeIfMatchRegex;
            cbOrig.RecordCondition.Selector = @"\w*";

            cbOrig.IgnoreCommentedLines.CommentMarker = "//";
            cbOrig.IgnoreCommentedLines.InAnyPlace = false;

            cbOrig.IgnoreEmptyLines = true;
            cbOrig.IgnoreFirstLines = 123;
            cbOrig.IgnoreLastLines = 456;

            cbOrig.SealedClass = false;
            string xml = cbOrig.SaveToXmlString();

            ClassBuilder cb2 = ClassBuilder.LoadFromXmlString(xml);

            Assert.AreEqual("Customers", cb2.ClassName);
            Assert.AreEqual(2, cb2.FieldCount);
            Assert.AreEqual("Field1", cb2.Fields[0].FieldName);

            Assert.AreEqual(RecordCondition.ExcludeIfMatchRegex, cb2.RecordCondition.Condition);
            Assert.AreEqual(@"\w*", cb2.RecordCondition.Selector);

            Assert.AreEqual("//", cb2.IgnoreCommentedLines.CommentMarker);
            Assert.AreEqual(false, cb2.IgnoreCommentedLines.InAnyPlace);
            Assert.AreEqual(false, cb2.SealedClass);

            Assert.AreEqual(true, cb2.IgnoreEmptyLines);
            Assert.AreEqual(123, cb2.IgnoreFirstLines);
            Assert.AreEqual(456, cb2.IgnoreLastLines);
        }
Exemplo n.º 15
0
        public void RunTimeGenerics()
        {
            var cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.AddField("Field2", typeof (Dictionary<int, List<string>>));
            cb.AddField("Field1", "System.Collections.Generic.List<int>");
            cb.AddField("Field7", typeof (List<int>));
            cb.AddField("Field3", "Nullable<int>");
            cb.AddField("Field4", typeof (Nullable<int>));

            cb.CreateRecordClass();
        }
Exemplo n.º 16
0
        public void RunTimeGenerics()
        {
            DelimitedClassBuilder cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.AddField("Field2", typeof(Dictionary<int, List<string>>));
            cb.AddField("Field1", "List<int>");
            cb.AddField("Field2", typeof(List<int>));
            cb.AddField("Field3", "Nullable<int>");
            cb.AddField("Field4", typeof(Nullable<int>));
        }
Exemplo n.º 17
0
        private static void Main(string[] args)
        {
            /*var provider = new ExcelStorage(typeof(RaRecord)) {
                StartRow = 2,
                StartColumn = 1,
                SheetName = "Sheet2",
                FileName = "test.xlsx"
            };*/

            //Dynamic Records
            var cb = new DelimitedClassBuilder("Customer", "|")
            {
                IgnoreFirstLines = 1,
                IgnoreEmptyLines = true
            };

            cb.AddField("BirthDate", typeof(DateTime));
            cb.LastField.TrimMode = TrimMode.Both;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("Name", typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar = '"';

            cb.AddField("Age", typeof(int));           

            var providerWithDynamicRecord = new ExcelNPOIStorage(cb.CreateRecordClass())
            {                
                FileName =Directory.GetCurrentDirectory()+@"\testDynamicRecords.xlsx"
            };

            providerWithDynamicRecord.StartRow = 1;
            providerWithDynamicRecord.StartColumn = 1;

            dynamic dynamicRecord = Activator.CreateInstance(providerWithDynamicRecord.RecordType);

            dynamicRecord.Name = "Jonh";
            dynamicRecord.Age = 1;
            dynamicRecord.BirthDate = DateTime.Now;

            var valuesList = new List<dynamic>
            {
                dynamicRecord,
                dynamicRecord
            };

            var columnsHeaders = ((System.Reflection.TypeInfo)(dynamicRecord.GetType())).DeclaredFields.Select(x => x.Name).ToList();
            providerWithDynamicRecord.ColumnsHeaders = columnsHeaders;
            providerWithDynamicRecord.InsertRecords(valuesList.ToArray());            

            //General export of excel with date time columns
            var provider = new ExcelNPOIStorage(typeof (RaRecord))
            {
                FileName = Directory.GetCurrentDirectory() + @"\test.xlsx"
            };

            provider.StartRow = 0;
            provider.StartColumn = 0;

            var records = new List<RaRecord>();

            records.Add(new RaRecord()
            {
                Level = 123.123m,
                Name = "Dickie",
                Startdate = DateTime.Now
            });

            var values = new List<int>
            {
                1,
                2,
                3
            };

            records.Add(new RaRecord()
            {
                Level = null,
                Name = "Bavo",
                Project = "too many",
                Startdate = DateTime.Now,
                ListOfIds = string.Join(",", values.Select(n => n.ToString(CultureInfo.InvariantCulture)).ToArray())
            });            
            
            provider.HeaderRows = 4;
                       
            provider.InsertRecords(records.ToArray());

            var res = (RaRecord[])provider.ExtractRecords();

        }