public static SnData LoadFromXlsx(string filename) { var snData = new SnData(); snData.Rows = new List <string[]>(); var workbook = new XLWorkbook(filename); IXLWorksheet worksheet = workbook.Worksheets.First(); int i = 0; foreach (IXLRow row in worksheet.Rows()) { if (i == 0) { var header = new List <string>(); foreach (IXLCell cell in row.Cells()) { header.Add(cell.Value.ToString()); } snData.HeaderRow = header.ToArray(); if (snData.HeaderRow.Length == 0 || snData.HeaderRow[0] != SerialColumnName) { throw new SerialColumnException(); } } else { var values = new List <string>(); foreach (IXLCell cell in row.Cells()) { values.Add(cell.Value.ToString()); } if (values.Count() >= 1 && values[0].Trim() != string.Empty) { if (values.Count() != snData.HeaderRow.Length) { throw new InconsistentException(); } if (snData.Rows.Select(r => r[0]).Where(v => v == values[0]).Count() > 0) { throw new DuplicateSerialException(values[0]); } snData.Rows.Add(values.ToArray()); } } i++; } if (snData.Rows.Count == 0) { throw new EmptyException(); } return(snData); }
private void BrowseBtn_Click(object sender, RoutedEventArgs e) { RowsNumberLabel.Content = "..."; ReserveBtn.IsEnabled = false; FillSignBtn.IsEnabled = false; var dlg = new OpenFileDialog(); dlg.CheckFileExists = true; dlg.Filter = "Excel files|*.xlsx|CSV files|*.csv"; dlg.AddExtension = true; dlg.DefaultExt = "xlsx"; dlg.ValidateNames = true; if (dlg.ShowDialog() != null) { if (!string.IsNullOrEmpty(dlg.FileName)) { try { if (dlg.FileName.EndsWith(".xlsx")) { this.snData = SnData.LoadFromXlsx(dlg.FileName); } else { this.snData = SnData.LoadFromCsv(dlg.FileName); } RowsNumberLabel.Content = this.snData.Rows.Count; StatusTextBlock.Text = string.Empty; ReserveBtn.IsEnabled = true; FillSignBtn.IsEnabled = true; } catch (SnData.EmptyException) { StatusTextBlock.Text = "File is empty or header row missing"; StatusTextBlock.Foreground = this.errorColor; } catch (SnData.InconsistentException) { StatusTextBlock.Text = "Number of values in a data row does not correspond to the header row"; StatusTextBlock.Foreground = this.errorColor; } catch (SnData.SerialColumnException) { StatusTextBlock.Text = SnData.SerialColumnName + " column header missing"; StatusTextBlock.Foreground = this.errorColor; } catch (SnData.DuplicateSerialException ex) { StatusTextBlock.Text = "Duplicate serial number in the file: " + ex.Message; StatusTextBlock.Foreground = this.errorColor; } CsvFileText.Text = dlg.FileName; } } }
public static SnData LoadFromCsv(string filename) { var snData = new SnData(); using (var reader = new StreamReader(File.OpenRead(filename))) { string line = reader.ReadLine(); if (reader.EndOfStream) { throw new EmptyException(); } snData.HeaderRow = line.Split(separator); if (snData.HeaderRow.Length == 0 || snData.HeaderRow[0] != SerialColumnName) { throw new SerialColumnException(); } snData.Rows = new List <string[]>(); while (!reader.EndOfStream) { line = reader.ReadLine(); if (line.Trim() != string.Empty) { string[] values = line.Split(separator); if (values.Length != snData.HeaderRow.Length) { throw new InconsistentException(); } if (snData.Rows.Select(r => r[0]).Where(v => v == values[0]).Count() > 0) { throw new DuplicateSerialException(values[0]); } snData.Rows.Add(values); } } } return(snData); }