diff --git a/ENI2/DetailViewControls/WasteDetailControl.xaml.cs b/ENI2/DetailViewControls/WasteDetailControl.xaml.cs index 140681b5..2a8d9c60 100644 --- a/ENI2/DetailViewControls/WasteDetailControl.xaml.cs +++ b/ENI2/DetailViewControls/WasteDetailControl.xaml.cs @@ -5,6 +5,7 @@ using bsmd.database; using ClosedXML.Excel; using ENI2.EditControls; +using ENI2.Excel; using ENI2.Util; using Microsoft.Win32; using System; @@ -479,88 +480,14 @@ namespace ENI2.DetailViewControls private void buttonImportFromExcel_Click(object sender, RoutedEventArgs e) { - OpenFileDialog ofd = new OpenFileDialog(); - ofd.Filter = "Excel Files|*.xls;*.xlsx"; - if (ofd.ShowDialog() ?? false) + int importWasteListCnt = ExcelLocalImportHelper.ImportWaste(_was); + + if (importWasteListCnt > 0) { - try - { - using (var stream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - using (var workbook = new XLWorkbook(stream)) - { - var worksheet = workbook.Worksheet(1); // Get first worksheet - var rows = worksheet.RangeUsed().RowsUsed().Skip(3); // Skip first three rows - - List importWasteList = new List(); - - int cnt = 0; - object o = null; - - // Diese Funktion kann das "alte" Sheet Format nicht mehr einlesen! - - foreach (var row in rows) - { - if (cnt >= 35) break; // Maximum 35 rows - - if (worksheet.RangeUsed().ColumnCount() < 9) - { - throw new InvalidDataException("Sheet must have 9 Columns of data"); - } - - if (!row.Cell(2).IsEmpty()) o = row.Cell(2).Value; else o = null; - if ((o != null) && Int32.TryParse(o.ToString(), out int wasteType)) - { - Waste waste = _was.GetWasteForType(wasteType); - if (waste == null) - { - waste = new Waste(); - waste.WasteType = wasteType; - waste.WAS = this._was; - waste.IsDirty = true; - waste.Identifier = Waste.GetNewIdentifier(this._was.Waste); - this._was.Waste.Add(waste); - } - else - { - waste.IsDirty = true; - } - - if (!row.Cell(5).IsEmpty()) waste.WasteDescription = row.Cell(5).GetString(); - if (waste.WasteDescription.IsNullOrEmpty()) - waste.WasteDescription = "-"; - - if (!row.Cell(6).IsEmpty()) o = row.Cell(6).Value; else o = null; - if (o != null) waste.WasteDisposalAmount_MTQ = Convert.ToDouble(o); - - if (!row.Cell(7).IsEmpty()) o = row.Cell(7).Value; else o = null; - if (o != null) waste.WasteCapacity_MTQ = Convert.ToDouble(o); - - if (!row.Cell(8).IsEmpty()) o = row.Cell(8).Value; else o = null; - if (o != null) waste.WasteAmountRetained_MTQ = Convert.ToDouble(o); - - if (!row.Cell(9).IsEmpty()) waste.WasteDisposalPort = row.Cell(9).GetString().ToUpper(); - - if (!row.Cell(10).IsEmpty()) o = row.Cell(10).Value; else o = null; - if (o != null) waste.WasteAmountGeneratedTillNextPort_MTQ = Convert.ToDouble(o); - - importWasteList.Add(waste); - cnt++; - } - } - - if (importWasteList.Count > 0) - { - this.dataGridWaste.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.WAS); - MessageBox.Show(String.Format(Properties.Resources.textWasteImported, importWasteList.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - } + this.dataGridWaste.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.WAS); + MessageBox.Show(String.Format(Properties.Resources.textWasteImported, importWasteListCnt), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); + } } diff --git a/ENI2/Excel/ExcelLocalImportHelper.cs b/ENI2/Excel/ExcelLocalImportHelper.cs index e013ae6e..88dac012 100644 --- a/ENI2/Excel/ExcelLocalImportHelper.cs +++ b/ENI2/Excel/ExcelLocalImportHelper.cs @@ -4,6 +4,7 @@ using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Windows; namespace ENI2.Excel @@ -41,7 +42,7 @@ namespace ENI2.Excel List importL10C = new List(); - + foreach (var row in rows) { if (cnt >= 10) break; // Maximum 10 rows @@ -115,7 +116,7 @@ namespace ENI2.Excel { throw new InvalidDataException($"Error processing row {row.RowNumber()}: {ex.Message}", ex); } - } + } } } catch (Exception ex) @@ -229,7 +230,7 @@ namespace ENI2.Excel poc30.MDH = mdh; mdh.PortOfCallLast30Days.Add(poc30); importPoC30.Add(poc30); - } + } } } catch (Exception ex) @@ -244,5 +245,89 @@ namespace ENI2.Excel #endregion + #region Waste from Excel + + public static int ImportWaste(WAS was) + { + int cnt = 0; + OpenFileDialog ofd = new OpenFileDialog(); + ofd.Filter = "Excel Files|*.xls;*.xlsx"; + if (ofd.ShowDialog() ?? false) + { + try + { + using (var stream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using (var workbook = new XLWorkbook(stream)) + { + var worksheet = workbook.Worksheet(1); // Get first worksheet + var rows = worksheet.RangeUsed().RowsUsed().Skip(3); // Skip first three rows + + List importWasteList = new List(); + + object o = null; + + // Diese Funktion kann das "alte" Sheet Format nicht mehr einlesen! + + foreach (var row in rows) + { + if (cnt >= 35) break; // Maximum 35 rows + + if (worksheet.RangeUsed().ColumnCount() < 9) + { + throw new InvalidDataException("Sheet must have 9 Columns of data"); + } + + if (!row.Cell(2).IsEmpty()) o = row.Cell(2).Value; else o = null; + if ((o != null) && Int32.TryParse(o.ToString(), out int wasteType)) + { + Waste waste = was.GetWasteForType(wasteType); + if (waste == null) + { + waste = new Waste(); + waste.WasteType = wasteType; + waste.WAS = was; + waste.IsDirty = true; + waste.Identifier = Waste.GetNewIdentifier(was.Waste); + was.Waste.Add(waste); + } + else + { + waste.IsDirty = true; + } + + if (!row.Cell(5).IsEmpty()) waste.WasteDescription = row.Cell(5).GetString(); + if (waste.WasteDescription.IsNullOrEmpty()) + waste.WasteDescription = "-"; + + if (!row.Cell(6).IsEmpty()) + waste.WasteDisposalAmount_MTQ = row.Cell(6).GetDouble(); + + if (!row.Cell(7).IsEmpty()) + waste.WasteCapacity_MTQ = row.Cell(7).GetDouble(); + + if (!row.Cell(8).IsEmpty()) waste.WasteAmountRetained_MTQ = row.Cell(8).GetDouble(); + + if (!row.Cell(9).IsEmpty()) + waste.WasteDisposalPort = row.Cell(9).GetString().ToUpper(); + + if (!row.Cell(10).IsEmpty()) + waste.WasteAmountGeneratedTillNextPort_MTQ = row.Cell(10).GetDouble(); + + importWasteList.Add(waste); + cnt++; + } + } + } + } + catch (Exception ex) + { + MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); + } + } + return cnt; + } + + #endregion + } } diff --git a/ENI2/SheetDisplayControls/PortControl.xaml.cs b/ENI2/SheetDisplayControls/PortControl.xaml.cs index 2a9e957d..f061d0c6 100644 --- a/ENI2/SheetDisplayControls/PortControl.xaml.cs +++ b/ENI2/SheetDisplayControls/PortControl.xaml.cs @@ -5,6 +5,7 @@ using bsmd.database; using ClosedXML.Excel; using ENI2.EditControls; +using ENI2.Excel; using Microsoft.Win32; using System; @@ -514,89 +515,14 @@ namespace ENI2.SheetDisplayControls private void buttonImportFromExcel_Click(object sender, RoutedEventArgs e) { - OpenFileDialog ofd = new OpenFileDialog(); - ofd.Filter = "Excel Files|*.xls;*.xlsx"; - if (ofd.ShowDialog() ?? false) + int importWasteListCnt = ExcelLocalImportHelper.ImportWaste(_was); + + if (importWasteListCnt > 0) { - try - { - using (var stream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - using (var workbook = new XLWorkbook(stream)) - { - var worksheet = workbook.Worksheet(1); // Get first worksheet - var rows = worksheet.RangeUsed().RowsUsed().Skip(3); // Skip first three rows - - List importWasteList = new List(); - - int cnt = 0; - - // Diese Funktion kann das "alte" Sheet Format nicht mehr einlesen! - - foreach (var row in rows) - { - if (cnt >= 35) break; // Maximum 35 rows - - if (worksheet.RangeUsed().ColumnCount() < 9) - { - throw new InvalidDataException("Sheet must have 9 Columns of data"); - } - - object o = null; - - if (!row.Cell(2).IsEmpty()) o = row.Cell(2).Value; else o = null; - if ((o != null) && Int32.TryParse(o.ToString(), out int wasteType)) - { - Waste waste = _was.GetWasteForType(wasteType); - if (waste == null) - { - waste = new Waste(); - waste.WasteType = wasteType; - waste.WAS = this._was; - waste.IsDirty = true; - waste.Identifier = Waste.GetNewIdentifier(this._was.Waste); - this._was.Waste.Add(waste); - } - else - { - waste.IsDirty = true; - } - - if (!row.Cell(5).IsEmpty()) waste.WasteDescription = row.Cell(5).GetString(); - if (waste.WasteDescription.IsNullOrEmpty()) - waste.WasteDescription = "-"; - - if (!row.Cell(6).IsEmpty()) o = row.Cell(6).Value; else o = null; - if (o != null) waste.WasteDisposalAmount_MTQ = Convert.ToDouble(o); - - if (!row.Cell(7).IsEmpty()) o = row.Cell(7).Value; else o = null; - if (o != null) waste.WasteCapacity_MTQ = Convert.ToDouble(o); - - if (!row.Cell(8).IsEmpty()) o = row.Cell(8).Value; else o = null; - if (o != null) waste.WasteAmountRetained_MTQ = Convert.ToDouble(o); - - if (!row.Cell(9).IsEmpty()) waste.WasteDisposalPort = row.Cell(9).GetString().ToUpper(); - - if (!row.Cell(10).IsEmpty()) o = row.Cell(10).Value; else o = null; - if (o != null) waste.WasteAmountGeneratedTillNextPort_MTQ = Convert.ToDouble(o); - - importWasteList.Add(waste); - cnt++; - } - } - - if (importWasteList.Count > 0) - { - this.dataGridWaste.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.WAS); - MessageBox.Show(String.Format(Properties.Resources.textWasteImported, importWasteList.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - } + this.dataGridWaste.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.WAS); + MessageBox.Show(String.Format(Properties.Resources.textWasteImported, importWasteListCnt), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); + } } #endregion