Esempio n. 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);
            }
        }
        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);
            }
        }