Exemplo n.º 1
0
        public void Iterator_MapsCorrectData_WhenMixedConfigurationPassedAsAnArgument()
        {
            var builder = ExcelIteratorConfigurationBuilder <ExampleRow> .Default();

            builder.FirstRowContainsColumnNames(flag: true);
            builder.SkipEmptyColumnNames(flag: true);
            builder.MapByName(p => p.FirstName, "First NAME");
            builder.MapByIndex(p => p.LastName, 2);
            builder.Ignore(p => p.Address);
            builder.MapByName(p => p.IsActive, "is AcTIve");
            builder.ConvertSourceValue(p => p.IsActive, sourceValue => sourceValue?.ToString() == "t");

            var config = builder.Build();


            using var iterator = ExcelIteratorCreator.Create <ExampleRow>(GetFileStream("Example.xlsx"), config);

            var items     = iterator.ToList();
            var firstItem = iterator.First();

            Assert.Equal(3, items.Count);

            Assert.Equal(1, firstItem.Id);
            Assert.Equal("Unknown", firstItem.FirstName);
            Assert.Equal("User", firstItem.LastName);
            Assert.True(string.IsNullOrWhiteSpace(firstItem.Address));
            Assert.Equal(new DateTime(1990, 3, 7), firstItem.Date);
            Assert.True(firstItem.IsActive);
        }
        /// <summary>
        /// Creates a new instance of <see cref="IExcelIterator{T}"/> with optional action for defining configuration
        /// </summary>
        /// <param name="fileStream">The stream of an excel file</param>
        /// <param name="builderAction">The optional action for defining configuration
        /// If not specified, the default configuration will be used.</param>
        /// <typeparam name="T">The type which represents an item (row) from excel file</typeparam>
        /// <returns>A new instance of <see cref="IExcelIterator{T}"/></returns>
        public static IExcelIterator <T> Create <T>(Stream fileStream,
                                                    Action <ExcelIteratorConfigurationBuilder <T> > builderAction = null)
            where T : class, new()
        {
            var configurationBuilder = ExcelIteratorConfigurationBuilder <T> .Default();

            builderAction?.Invoke(configurationBuilder);

            return(new ExcelIterator <T>(fileStream, configurationBuilder.Build()));
        }
 /// <summary>
 /// Creates a new instance of <see cref="IExcelIterator{T}"/> with optional configuration
 /// </summary>
 /// <param name="fileStream">The stream of an excel file</param>
 /// <param name="configuration">The optional configuration.
 /// If not specified the default configuration will be used</param>
 /// <typeparam name="T">The type which represents an item (row) from excel file</typeparam>
 /// <returns>A new instance of <see cref="IExcelIterator{T}"/></returns>
 public static IExcelIterator <T> Create <T>(Stream fileStream, ExcelIteratorConfiguration configuration = null)
     where T : class, new()
 {
     return(new ExcelIterator <T>(fileStream, configuration ?? ExcelIteratorConfigurationBuilder <T> .Default().Build()));
 }