예제 #1
0
        /// <summary>
        ///     Checks and throws the <see cref="ExcelValidationException"/> if header columns does not match with properties of object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="worksheet"></param>
        /// <param name="headerRowIndex"></param>
        /// <param name="formattedExceptionMessage"></param>
        public static void CheckHeadersAndThrow <T>(this ExcelWorksheet worksheet, int headerRowIndex, string formattedExceptionMessage = null)
        {
            List <KeyValuePair <PropertyInfo, ExcelTableColumnAttribute> > propertyAttributePairs = typeof(T).GetExcelTableColumnAttributes <T>();

            for (var i = 0; i < propertyAttributePairs.Count; i++)
            {
                string columnName = !string.IsNullOrEmpty(propertyAttributePairs[i].Value.ColumnName) ? propertyAttributePairs[i].Value.ColumnName : propertyAttributePairs[i].Key.Name;
                worksheet.CheckAndThrowColumn(headerRowIndex, i + 1, columnName, formattedExceptionMessage);
            }
        }
        public void Test_CheckAndThrowColumn()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["TEST6"];

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action1 = () => { worksheet.CheckAndThrowColumn(1, 3, "Barcode", "Barcode column is missing"); };

            Action action2 = () => { worksheet.CheckAndThrowColumn(1, 1, "Barcode"); };

            Action action3 = () => { worksheet.CheckAndThrowColumn(2, 14, "Barcode"); };

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action1.Should().Throw <ExcelTableValidationException>().And.Message.Should().Be("Barcode column is missing");
            action2.Should().NotThrow();
            action3.Should().Throw <ExcelTableValidationException>();
        }