Пример #1
0
        protected override void LoadFromFile(string path)
        {
            var separator = _csvSeparatorSelector.GetCsvSeparator(path);

            //if separator selection dialog was cancelled, abort
            if (!(separator is char separatorCharacter))
            {
                return;
            }

            //we keep a copy of the already loaded sheets, in case the reading fails
            var alreadyLoadedDataSheets = DataSheets.Clone();

            DataSheets.Clear();

            try
            {
                using (var reader = new CsvReaderDisposer(path, separatorCharacter))
                {
                    var csv     = reader.Csv;
                    var headers = csv.GetFieldHeaders();
                    var rows    = new List <List <string> >(headers.Length);

                    var dataSheet = new DataSheet
                    {
                        SheetName = ""
                    };

                    for (var i = 0; i < headers.Length; i++)
                    {
                        dataSheet.AddColumn(headers[i], i);
                    }
                    var currentRow = new string[csv.FieldCount];

                    while (csv.ReadNextRecord())
                    {
                        csv.CopyCurrentRecordTo(currentRow);
                        var rowList = currentRow.ToList();
                        var levels  = getMeasurementLevels(rowList);
                        dataSheet.CalculateColumnDescription(levels);
                        dataSheet.AddRow(rowList);
                    }

                    dataSheet.RemoveEmptyColumns();
                    dataSheet.RemoveEmptyRows();

                    DataSheets.AddSheet(dataSheet);
                }
            }
            catch (Exception e)
            {
                DataSheets.CopySheetsFrom(alreadyLoadedDataSheets);
                _logger.AddError(e.Message);
                throw new InvalidObservedDataFileException(e.Message);
            }
        }
Пример #2
0
        protected override void LoadFromFile(string path)
        {
            //we keep a copy of the already loaded sheets, in case the reading fails
            var alreadyLoadedDataSheets = DataSheets.Clone();

            DataSheets.Clear();

            try
            {
                var reader = new ExcelReader(path);

                while (reader.MoveToNextSheet())
                {
                    if (!reader.MoveToNextRow())
                    {
                        continue;
                    }

                    var rawSheetData = new DataSheet
                    {
                        SheetName = reader.CurrentSheet.SheetName
                    };
                    var headers = reader.CurrentRow;

                    for (var j = 0; j < headers.Count; j++)
                    {
                        rawSheetData.AddColumn(headers[j], j);
                    }

                    while (reader.MoveToNextRow())
                    {
                        //the first two could even be done only once
                        var levels = reader.GetMeasurementLevels(headers.Count);
                        rawSheetData.CalculateColumnDescription(levels);
                        rawSheetData.AddRow(reader.CurrentRow);
                    }

                    rawSheetData.RemoveEmptyColumns();
                    rawSheetData.RemoveEmptyRows();

                    DataSheets.AddSheet(rawSheetData);
                }
            }
            catch (Exception ex)
            {
                DataSheets.CopySheetsFrom(alreadyLoadedDataSheets);
                _logger.AddError(ex.Message);
                throw new InvalidObservedDataFileException(ex.Message);
            }
        }
 public override void GlobalContext()
 {
     base.GlobalContext();
     _dataSourceFile = A.Fake <IDataSourceFile>();
     _importer       = A.Fake <IImporter>();
     _view           = A.Fake <IImporterDataView>();
     _dataSheet      = new DataSheet {
         SheetName = "sheet1"
     };
     _dataSheet.AddColumn("test_column", 0);
     _dataSheet.AddRow(new List <string>()
     {
         "1"
     });
     _dataSheet2 = new DataSheet {
         SheetName = "sheet2"
     };
     _dataSheet2.AddColumn("test_column", 0);
     _dataSheet2.AddRow(new List <string>()
     {
         "1"
     });
     _dataSheet3 = new DataSheet {
         SheetName = "sheet3"
     };
     _dataSheet3.AddColumn("test_column", 0);
     _dataSheet3.AddRow(new List <string>()
     {
         "1"
     });
     _sheetsCollection = new DataSheetCollection();
     _sheetsCollection.AddSheet(_dataSheet);
     _sheetsCollection.AddSheet(_dataSheet2);
     _sheetsCollection.AddSheet(_dataSheet3);
     A.CallTo(() => _importer.LoadFile(A <ColumnInfoCache> ._, A <string> ._, A <IReadOnlyList <MetaDataCategory> > ._)).Returns(_dataSourceFile);
     A.CallTo(() => _view.GetActiveFilterCriteria()).Returns("active_filter_criteria");
     A.CallTo(() => _dataSourceFile.DataSheets).Returns(_sheetsCollection);
 }