public void ItShouldExtractRowsAndHeader_GivenExcelFileWithMultipleSheets()
        {
            var target = new GenericReadExcel { HasHeader = true };
            IDictionary<string, IEnumerable<Dictionary<string, string>>> actual;

            using (Stream stream = new FileStream("ComplexBook.xlsx", FileMode.Open))
            {
                actual = target.ExtractRows(stream, (dictionary, row) =>
                {
                    var extractedRow = new Dictionary<string, string>();
                    foreach (var header in dictionary)
                    {
                        extractedRow[header.Value] = row.GetValue<string>(header.Key);
                    }

                    return extractedRow;

                });
            }

            actual.Count.Should().Be(2);
            actual.ContainsKey("col").Should().BeTrue();
            actual.ContainsKey("book").Should().BeTrue();
            actual["col"].Count().Should().Be(2);
            actual["book"].Last()["Name"].Should().Be("B");
        }
        public void CreateExcelFileTest()
        {
            const string filename = "SimpleBook2.xlsx";
            var target = new GenericWriteExcel();
            var rows = SimpleExcelRowFactory.CreateSimpleRows();

            var actual = target.CreateExcelFile(rows);
            using (var file = File.OpenWrite(filename))
            {
                file.Write(actual.GetBuffer(), 0, (int) actual.Length);
            }

            using (var file = File.OpenRead(filename))
            {
                var excel = new GenericReadExcel {HasHeader = true};
                var actualRows = excel.ExtractRows(file, (header, row) => new SimpleExcelRow
                {
                    Col1 = row.GetValue<int>(1),
                    Col2 = row.GetValue<string>(2),
                    Col3 = row.GetValue<DateTime>(3)
                }, 1);
                var actualRowsList = new List<SimpleExcelRow>();
                actualRowsList.AddRange(actualRows);

                AssertEx.PropertyValuesAreEquals(actualRowsList, rows);
            }
        }
        public void ItShouldExtractRows_GivenSheetName()
        {
            var target = new GenericReadExcel { HasHeader = true };
            const string sheetName = "Sheet1";
            Func<Dictionary<int, string>, ExcelWorksheetRow, SimpleExcelRow> rowFunction = ReadOneRow;
            var expected = SimpleExcelRowFactory.CreateSimpleRows();
            var actual = new List<SimpleExcelRow>();

            using (Stream stream = new FileStream("SimpleBook.xlsx", FileMode.Open))
            {
                actual.AddRange(target.ExtractRows(stream, rowFunction, sheetName));
            }

            AssertEx.PropertyValuesAreEquals(actual, expected);
        }
        public void ItShouldExtractRows_GivenSheetNumber()
        {
            var expected = SimpleExcelRowFactory.CreateSimpleRows();

            var target = new GenericReadExcel { HasHeader = true };
            var excelRows = new List<SimpleExcelRow>();

            using (Stream stream = new FileStream("SimpleBook.xlsx", FileMode.Open))
            {
                excelRows.AddRange(target.ExtractRows(stream,
                    (header, row) => new SimpleExcelRow
                    {
                        Col1 = row.GetValue<int>(1),
                        Col2 = row.GetValue<string>(2),
                        Col3 = row.GetValue<DateTime>(3)
                    }, 1));
            }

            AssertEx.PropertyValuesAreEquals(excelRows, expected);
        }