private void DoMapRowsToPropertiesWorkButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LogMessage("I'm using RAW read service (ClassToExcelRawReaderService) to get data and then " +
                           "I'm using a converter (ClassToExcelRowConverter) to map rows to a class' properties.");
                LogMessage("Note that blank rows are skipped by OpenXml.");

                // Load the embedded Beverages.xlsx file.
                String someDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string filePath      = Path.Combine(someDirectory, "Beverages.xlsx");
                if (File.Exists(filePath))
                {
                    LogMessage($"Loading data from {filePath}");
                    // Get tab names
                    List <string> worksheetNames = string.IsNullOrWhiteSpace(WorksheetNames.Text)
                        ? new List <string>()
                        : WorksheetNames.Text.Split(',').ToList();

                    if (worksheetNames.Count > 0)
                    {
                        var worksheetName = worksheetNames.First().Trim();

                        // Passing in LogServiceMessage in optional.  I'm using it here to help debug in issues while reading a file.
                        var rawReaderService = new ClassToExcelRawReaderService(LogServiceMessage);
                        List <ClassToExcelRawRow> rawRows = rawReaderService.ReadWorksheet(filePath, worksheetName);
                        if (rawRows.Count > 0)
                        {
                            // Rows 2-6 (Column C) have the Beverage class data
                            var      converter1 = new ClassToExcelRowConverter <Beverage>(LogServiceMessage);
                            Beverage beverage   = converter1.Convert(rawRows);
                            LogMessage(SerializationHelper.SerializeToXml(beverage));

                            // Rows 8-9 (Columns B & C) have the BeverageDates class data
                            var           converter2 = new ClassToExcelRowConverter <BeverageDates>(LogServiceMessage);
                            BeverageDates dates      = converter2.Convert(rawRows);
                            LogMessage(SerializationHelper.SerializeToXml(dates));
                        }
                        else
                        {
                            LogMessage("No data found.");
                        }
                    }
                    else
                    {
                        LogMessage("No work sheet (tab) names specified");
                    }
                }
                else
                {
                    LogMessage($"The file does not exists: {filePath}");
                }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
Exemplo n.º 2
0
        public void Convert_CanMapRowsToPrivatePropeties_ValuesTransferCorrectly()
        {
            // Arrange
            var rows = CreateRows();

            // Act
            var sut          = new ClassToExcelRowConverter <Beverage>(null);
            var actualResult = sut.Convert(rows);

            // Assert
            Assert.AreEqual(5, actualResult.DrPepper, "Unable to assign a value to a private property!");
        }
Exemplo n.º 3
0
        public void Convert_CanParseDifferentRowsToDifferentClass_ValuesTransferCorrectly()
        {
            // Arrange
            var rows = CreateRows();

            // Act
            var sut          = new ClassToExcelRowConverter <BeverageDates>(null);
            var actualResult = sut.Convert(rows);

            // Assert
            Assert.AreEqual(new DateTime(2014, 8, 31), actualResult.BeerStart);
            Assert.AreEqual(new DateTime(2014, 9, 1), actualResult.BeerEnd);
            Assert.AreEqual(new DateTime(2014, 10, 31), actualResult.WineStart);
            Assert.AreEqual(new DateTime(2014, 11, 1), actualResult.WineEnd);
        }
Exemplo n.º 4
0
        public void Convert_CanMapRowsToPropeties_ValuesTransferCorrectly()
        {
            // Arrange
            var rows = CreateRows();

            // Act
            var sut          = new ClassToExcelRowConverter <Beverage>(null);
            var actualResult = sut.Convert(rows);

            // Assert
            Assert.AreEqual(1, actualResult.Beer);
            Assert.AreEqual(2, actualResult.Wine);
            Assert.AreEqual(3, actualResult.Pepsi);
            Assert.AreEqual(4, actualResult.Coke);
        }
Exemplo n.º 5
0
        public void Convert_CanObtainPricesToDesiredPrecision_ValuesTransferCorrectly()
        {
            // Arrange
            var rows = CreateRows();

            // Act
            var sut          = new ClassToExcelRowConverter <Beverage>(null);
            var actualResult = sut.Convert(rows);

            // Assert
            Assert.AreEqual(2.35m, actualResult.BeerPrice);
            Assert.AreEqual(12.34m, actualResult.WinePrice);
            Assert.AreEqual(1.25m, actualResult.PepsiPrice);
            Assert.AreEqual(1.26m, actualResult.CokePrice);
            Assert.AreEqual(1.27m, actualResult.DrPepperPrice);
            Assert.AreEqual(0.8, actualResult.AvgNumberOfLiters);
        }