From b1b178b0f421821808d9c1f11584cbc96e7203bb Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Sat, 4 Oct 2025 09:31:40 +0200 Subject: [PATCH] Replaced Waste manual excel import with ClosedXML implementation --- ENI2/SheetDisplayControls/PortControl.xaml.cs | 165 ++++++++---------- 1 file changed, 74 insertions(+), 91 deletions(-) diff --git a/ENI2/SheetDisplayControls/PortControl.xaml.cs b/ENI2/SheetDisplayControls/PortControl.xaml.cs index 4770b831..60e86ce8 100644 --- a/ENI2/SheetDisplayControls/PortControl.xaml.cs +++ b/ENI2/SheetDisplayControls/PortControl.xaml.cs @@ -3,11 +3,11 @@ // using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; -using ExcelDataReader; using Microsoft.Win32; + using System; -using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -517,102 +517,85 @@ namespace ENI2.SheetDisplayControls ofd.Filter = "Excel Files|*.xls;*.xlsx"; if (ofd.ShowDialog() ?? false) { - FileStream stream; try { - stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read); + using (var workbook = new XLWorkbook(ofd.FileName)) + { + 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(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); - return; + MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); } - - using (var reader = ExcelReaderFactory.CreateReader(stream)) - { - List importWasteList = new List(); - - try - { - do - { - // skip first three rows - reader.Read(); - reader.Read(); - reader.Read(); - - int cnt = 0; - object o = null; - - // Diese Funktion kann das "alte" Sheet Format nicht mehr einlesen! - - while (reader.Read() && (cnt < 35)) - { - if (reader.FieldCount < 9) - { - throw new InvalidDataException("Sheet must have 9 Columns of data"); - } - - if (!reader.IsDBNull(1)) o = reader.GetValue(1); 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 (!reader.IsDBNull(4)) waste.WasteDescription = reader.GetString(4); - if (waste.WasteDescription.IsNullOrEmpty()) - waste.WasteDescription = "-"; - - if (!reader.IsDBNull(5)) o = reader.GetValue(5); else o = null; - if (o != null) waste.WasteDisposalAmount_MTQ = Convert.ToDouble(o); - - if (!reader.IsDBNull(6)) o = reader.GetValue(6); else o = null; - if (o != null) waste.WasteCapacity_MTQ = Convert.ToDouble(o); - - if (!reader.IsDBNull(7)) o = reader.GetValue(7); else o = null; - if (o != null) waste.WasteAmountRetained_MTQ = Convert.ToDouble(o); - - if (!reader.IsDBNull(8)) waste.WasteDisposalPort = reader.GetString(8).ToUpper(); - - if (!reader.IsDBNull(9)) o = reader.GetValue(9); else o = null; - if (o != null) waste.WasteAmountGeneratedTillNextPort_MTQ = Convert.ToDouble(o); - - importWasteList.Add(waste); - cnt++; - } - } - - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - 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); - } - } - - stream.Close(); - } - } + } #endregion