Esempio n. 1
0
        private void ConfigureWorksheet(ExcelSource excelSource)
        {
            if (ReferenceEquals(_reader, null))
            {
                return;
            }

            var worksheetName = excelSource.Worksheet;

            if (string.IsNullOrWhiteSpace(worksheetName))
            {
                return;
            }

            var isWorksheetFound = false;

            do
            {
                isWorksheetFound = string.Equals(_reader.Name, worksheetName);
            } while (!isWorksheetFound && _reader.NextResult());

            if (!isWorksheetFound)
            {
                throw Log.ErrorAndCreateException <Exception>($"No worksheet with name: '{worksheetName}' in project data file");
            }
        }
Esempio n. 2
0
        private void ConfigureStartRange(ExcelSource excelSource)
        {
            var cellRange = excelSource.TopLeftCell;

            var columnRow = ReferenceHelper.ReferenceToColumnAndRow(cellRange);

            _startColumnIndex = columnRow[1] - 1;
            _startRowIndex    = columnRow[0] - 1;
        }
Esempio n. 3
0
        public static List <string> GetWorkseetsList(this ExcelSource excelSource)
        {
            Argument.IsNotNull(() => excelSource);

            var source = excelSource.ToString();

            if (string.IsNullOrEmpty(source))
            {
                return(new List <string>());
            }

            var typeFactory = excelSource.GetTypeFactory();

            using (var reader = new ExcelReader(source))
            {
                return(reader.GetWorkseetsList());
            }
        }
Esempio n. 4
0
        private void Initialize(string source)
        {
            try
            {
                var excelSource = new ExcelSource(source);

                InitializeExcelReader(excelSource);
                ConfigureWorksheet(excelSource);
                ConfigureStartRange(excelSource);

#if DEBUG
                Log.Debug($"Reader is initialized with '{source}'");
#endif
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Failed to initialize reader for data source '{Source}'");
                _reader?.Dispose();
                _reader = null;

                AddValidationError($"Failed to initialize reader: '{ex.Message}'");
            }
        }
Esempio n. 5
0
        private void InitializeExcelReader(ExcelSource excelSource)
        {
            var filePath = excelSource.FilePath;

            if (!File.Exists(filePath))
            {
                AddValidationError($"File '{filePath}' not found");
                return;
            }
#if NETCORE
            // Register additional encodings as they supported by default only in .NET Framework
            var encodingProvider = CodePagesEncodingProvider.Instance;
            Encoding.RegisterProvider(encodingProvider);
#endif
#pragma warning disable IDISP001 // Dispose created.
            // Note: need to keep this stream open as long as the reader lives
            var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
#pragma warning restore IDISP001 // Dispose created.

            var fileExtension = Path.GetExtension(filePath);

            _reader?.Dispose();

            _reader = string.Equals(fileExtension, ".xlsx")
                ? ExcelReaderFactory.CreateOpenXmlReader(stream)
                : ExcelReaderFactory.CreateBinaryReader(stream);

#pragma warning disable IDISP004 // Don't ignore created IDisposable.
            _reader.AsDataSet(new ExcelDataSetConfiguration
            {
                ConfigureDataTable = (_) => new ExcelDataTableConfiguration
                {
                    UseHeaderRow = false
                }
            });
#pragma warning restore IDISP004 // Don't ignore created IDisposable.
        }