From 7a7ea56c0f15ad1da3aca205d7415d42a2a23938 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Sat, 4 Oct 2025 10:01:59 +0200 Subject: [PATCH] Replaced ExcelDataReader with ClosedXML code in remaining functions --- ENI2/Controls/MaerskListControl.xaml.cs | 206 +++++++------- ...timeHealthDeclarationDetailControl.xaml.cs | 147 +++++----- .../SecurityDetailControl.xaml.cs | 140 +++++---- .../WasteDetailControl.xaml.cs | 174 ++++++------ ENI2/ENI2.csproj | 3 - .../CrewDepartureControl.xaml.cs | 116 ++++---- .../CrewPreArrivalControl.xaml.cs | 113 ++++---- .../PassengerDepartureControl.xaml.cs | 127 ++++----- .../PassengerPreArrivalControl.xaml.cs | 127 ++++----- .../VoyageControl.xaml.cs | 265 +++++++++--------- ENI2/Util/Extensions.cs | 18 +- ENI2/packages.config | 1 - 12 files changed, 670 insertions(+), 767 deletions(-) diff --git a/ENI2/Controls/MaerskListControl.xaml.cs b/ENI2/Controls/MaerskListControl.xaml.cs index 20e69a3b..a4d275b6 100644 --- a/ENI2/Controls/MaerskListControl.xaml.cs +++ b/ENI2/Controls/MaerskListControl.xaml.cs @@ -2,23 +2,23 @@ // Description: Request dbh ids for Maersk data lists // -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Windows; -using System.Windows.Controls; -using Microsoft.Win32; - using bsmd.database; -using ExcelDataReader; -using System.Collections.ObjectModel; +using ClosedXML.Excel; using ENI2.Excel; using ENI2.Locode; using ENI2.Util; + +using Microsoft.Win32; +using System; + +using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Diagnostics; +using System.IO; using System.Linq; using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; namespace ENI2.Controls { @@ -336,80 +336,72 @@ namespace ENI2.Controls }; if (ofd.ShowDialog() ?? false) { - FileStream stream; try { - stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); - } - catch (Exception ex) - { - MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); - return; - } - - using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) - { - List importData = new List(); - - bool isFirstRow = true; - int currentRow = 0; - bool imosAreOkay = true; - - try + using (var workbook = new XLWorkbook(ofd.FileName)) { - while (reader.Read()) + var worksheet = workbook.Worksheet(1); // Get first worksheet + var rows = worksheet.RangeUsed().RowsUsed().Skip(1); // Skip header row + + List importData = new List(); + + int currentRow = 1; // Start at 1 since we skip header + bool imosAreOkay = true; + + foreach (var row in rows) { currentRow++; - if (isFirstRow) - { - isFirstRow = false; // this must be a header row, skip - continue; - } - - if (reader.FieldCount < 13) + if (worksheet.RangeUsed().ColumnCount() < 13) { throw new InvalidDataException("Sheet must have 13 columns of data"); } MaerskData md = new MaerskData(); - if (!reader.IsDBNull(0)) + + if (!row.Cell(1).IsEmpty()) { - if (reader.GetFieldType(0) == typeof(DateTime)) + var cellValue = row.Cell(1).Value; + if (cellValue.IsDateTime) { - md.ETA = reader.GetDateTime(0); + var dateTime = cellValue.GetDateTime(); + md.ETA = dateTime; md.ColA = md.ETA.ToString(); } else { - md.ColA = reader.GetString(0); + md.ColA = row.Cell(1).GetString(); if (DateTime.TryParse(md.ColA, out DateTime aDateTime)) md.ETA = aDateTime; } } - if (!reader.IsDBNull(1)) md.ColB = reader.ReadAsString(1); - if (!reader.IsDBNull(2)) md.ColC = reader.ReadAsString(2); - if (!reader.IsDBNull(3)) md.ColD = reader.ReadAsString(3); - if (!reader.IsDBNull(4)) md.ColE = reader.ReadAsString(4); - if (!reader.IsDBNull(5)) md.ColF = reader.ReadAsString(5); - if (!reader.IsDBNull(6)) md.ColG = reader.ReadAsString(6); - if (!reader.IsDBNull(7)) md.ColH = reader.ReadAsString(7); - if (!reader.IsDBNull(8)) md.ColI = reader.ReadAsString(8); + + if (!row.Cell(2).IsEmpty()) md.ColB = row.Cell(2).GetString(); + if (!row.Cell(3).IsEmpty()) md.ColC = row.Cell(3).GetString(); + if (!row.Cell(4).IsEmpty()) md.ColD = row.Cell(4).GetString(); + if (!row.Cell(5).IsEmpty()) md.ColE = row.Cell(5).GetString(); + if (!row.Cell(6).IsEmpty()) md.ColF = row.Cell(6).GetString(); + if (!row.Cell(7).IsEmpty()) md.ColG = row.Cell(7).GetString(); + if (!row.Cell(8).IsEmpty()) md.ColH = row.Cell(8).GetString(); + if (!row.Cell(9).IsEmpty()) md.ColI = row.Cell(9).GetString(); + if (md.ColI != null) { if ((md.ColI.Contains("bremerhaven", StringComparison.OrdinalIgnoreCase) && this.PortLocode.Equals("DEWVN")) || (md.ColI.Contains("eurogate", StringComparison.OrdinalIgnoreCase) && this.PortLocode.Equals("DEBRV"))) throw new InvalidOperationException($"{md.ColI} found in import to {PortLocode}, this is probably an error. Aborting import"); } - if (!reader.IsDBNull(9)) md.ColJ = reader.ReadAsString(9); + + if (!row.Cell(10).IsEmpty()) md.ColJ = row.Cell(10).GetString(); if (md.ColJ == null) continue; if (!(md.ColJ.Equals("msk", StringComparison.OrdinalIgnoreCase) || md.ColJ.Equals("sgl", StringComparison.OrdinalIgnoreCase))) continue; // skip operator we are not interested in - if (!reader.IsDBNull(10)) md.ColK = reader.ReadAsString(10); - if (!reader.IsDBNull(11)) md.ColL = reader.ReadAsString(11); - if (!reader.IsDBNull(12)) md.ColM = reader.ReadAsString(12); - if (!reader.IsDBNull(13)) md.Remark = reader.ReadAsString(13); - - if(!md.ColF.IsNullOrEmpty()) + + if (!row.Cell(11).IsEmpty()) md.ColK = row.Cell(11).GetString(); + if (!row.Cell(12).IsEmpty()) md.ColL = row.Cell(12).GetString(); + if (!row.Cell(13).IsEmpty()) md.ColM = row.Cell(13).GetString(); + if (!row.Cell(14).IsEmpty()) md.Remark = row.Cell(14).GetString(); + + if (!md.ColF.IsNullOrEmpty()) { if (Int32.TryParse(md.ColF, out int imo)) { @@ -417,7 +409,7 @@ namespace ENI2.Controls { imosAreOkay = false; } - } + } else { imosAreOkay = false; @@ -435,71 +427,67 @@ namespace ENI2.Controls } importData.Add(md); - - if (isFirstRow) isFirstRow = false; } - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (imosAreOkay && importData.Count > 0) - { - busyControl.BusyState = Util.UIHelper.BusyStateEnum.BUSY; - - foreach (MaerskData md in importData) - { - if (this.maerskDataList.Contains(md)) - { - // update record with imported record - MaerskData foundData = this.maerskDataList.First((m) => (m.ColF != null) ? m.ColF.Equals(md.ColF) : (md.ColF == null) && - (m.ColG != null) ? m.ColG.Equals(md.ColG) : (md.ColG == null) && - (m.ColH != null) ? m.ColH.Equals(md.ColH) : (md.ColH == null)); - if ((foundData.MessageCore == null) || !(foundData.MessageCore.Cancelled ?? false)) - { - if (foundData.ETA.HasValue && ((foundData.ETA.Value - DateTime.Now).TotalSeconds > 0) && foundData.Update(md)) - foundData.Status = MaerskData.MDStatus.UPDATED; - } - } - else + if (imosAreOkay && importData.Count > 0) + { + busyControl.BusyState = Util.UIHelper.BusyStateEnum.BUSY; + + foreach (MaerskData md in importData) { - if (!md.ColM.IsNullOrEmpty()) + if (this.maerskDataList.Contains(md)) { - md.MessageCore = await DBManagerAsync.LoadCoreByVisitIdAsync(md.ColM); - if (md.MessageCore != null) + // update record with imported record + MaerskData foundData = this.maerskDataList.First((m) => (m.ColF != null) ? m.ColF.Equals(md.ColF) : (md.ColF == null) && + (m.ColG != null) ? m.ColG.Equals(md.ColG) : (md.ColG == null) && + (m.ColH != null) ? m.ColH.Equals(md.ColH) : (md.ColH == null)); + + if ((foundData.MessageCore == null) || !(foundData.MessageCore.Cancelled ?? false)) { - MaerskData existingMD = await DBManagerAsync.LoadMaerskDataForCoreAsync(md.MessageCore.Id.Value); - if(existingMD == null) - { - // we have a core but no MarskData for this import -> save it - md.MessageCoreId = (Guid) md.MessageCore.Id; - _ = DBManagerAsync.SaveAsync(md); - } - else - { - existingMD.Update(md); - _ = DBManagerAsync.SaveAsync(existingMD); - this.UpdateStatus(existingMD); - maerskDataList.Add(existingMD); - continue; // add existing instead of new one - } + if (foundData.ETA.HasValue && ((foundData.ETA.Value - DateTime.Now).TotalSeconds > 0) && foundData.Update(md)) + foundData.Status = MaerskData.MDStatus.UPDATED; } } + else + { + if (!md.ColM.IsNullOrEmpty()) + { + md.MessageCore = await DBManagerAsync.LoadCoreByVisitIdAsync(md.ColM); + if (md.MessageCore != null) + { + MaerskData existingMD = await DBManagerAsync.LoadMaerskDataForCoreAsync(md.MessageCore.Id.Value); + if (existingMD == null) + { + // we have a core but no MarskData for this import -> save it + md.MessageCoreId = (Guid)md.MessageCore.Id; + _ = DBManagerAsync.SaveAsync(md); + } + else + { + existingMD.Update(md); + _ = DBManagerAsync.SaveAsync(existingMD); + this.UpdateStatus(existingMD); + maerskDataList.Add(existingMD); + continue; // add existing instead of new one + } + } + } - this.UpdateStatus(md); - maerskDataList.Add(md); + this.UpdateStatus(md); + maerskDataList.Add(md); + } } - } - this.TimeFilterItemSource(); - busyControl.BusyState = Util.UIHelper.BusyStateEnum.NEUTRAL; + this.TimeFilterItemSource(); + busyControl.BusyState = Util.UIHelper.BusyStateEnum.NEUTRAL; - this.dataGridPOCores.Items.Refresh(); + this.dataGridPOCores.Items.Refresh(); + } } } - - stream.Close(); + catch (Exception ex) + { + MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); + } } } diff --git a/ENI2/DetailViewControls/MaritimeHealthDeclarationDetailControl.xaml.cs b/ENI2/DetailViewControls/MaritimeHealthDeclarationDetailControl.xaml.cs index d5d719b3..4ec5a4f7 100644 --- a/ENI2/DetailViewControls/MaritimeHealthDeclarationDetailControl.xaml.cs +++ b/ENI2/DetailViewControls/MaritimeHealthDeclarationDetailControl.xaml.cs @@ -2,18 +2,16 @@ // Description: MDH Meldung Bearbeitungsseite // -using System; -using System.Collections.Generic; -using System.Windows; -using System.Windows.Controls; -using Microsoft.Win32; -using System.IO; - +using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Util; -using bsmd.database; - -using ExcelDataReader; +using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.IO; +using System.Windows; +using System.Windows.Controls; namespace ENI2.DetailViewControls { @@ -289,80 +287,73 @@ namespace ENI2.DetailViewControls }; 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(); // Get all rows with data + + List importPoC30 = new List(); + + foreach (var row in rows) + { + if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break; + + if (worksheet.RangeUsed().ColumnCount() < 3) + { + throw new InvalidDataException("Sheet must have at least 3 Columns of data"); + } + + PortOfCallLast30Days poc30 = new PortOfCallLast30Days(); + + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) poc30.PortOfCallLast30DaysLocode = row.Cell(1).GetString(); + if (!row.Cell(2).IsEmpty()) poc30.PortOfCallLast30DaysDateOfDeparture = row.Cell(2).GetDateTime(); + + string boolString = ""; + if (!row.Cell(3).IsEmpty()) boolString = row.Cell(3).GetString(); + poc30.PortOfCallLast30DaysCrewMembersJoined = (boolString.Equals("y", StringComparison.OrdinalIgnoreCase) || + (boolString.Equals("yes", StringComparison.OrdinalIgnoreCase)) || + (boolString.Equals("j", StringComparison.OrdinalIgnoreCase))); + + if (worksheet.RangeUsed().ColumnCount() > 3) + { + string allNewCrew = row.Cell(4).IsEmpty() ? null : row.Cell(4).GetString()?.Trim(); + if (!allNewCrew.IsNullOrEmpty()) + { + string[] crewNames = allNewCrew.Split(',', ';'); + for (int i = 0; i < crewNames.Length; i++) + { + string crewName = crewNames[i].Trim(); + if (crewName.IsNullOrEmpty()) continue; + PortOfCallLast30DaysCrewJoinedShip poc30Crew = new PortOfCallLast30DaysCrewJoinedShip(); + poc30Crew.PortOfCallLast30DaysCrewJoinedShipName = crewName; + poc30Crew.PortOfCallLast30Days = poc30; + poc30.CrewJoinedShip.Add(poc30Crew); + } + } + } + + poc30.MDH = this._mdh; + this._mdh.PortOfCallLast30Days.Add(poc30); + importPoC30.Add(poc30); + } + + if (importPoC30.Count > 0) + { + this.dataGridPortOfCallLast30Days.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.MDH); + MessageBox.Show(String.Format(Properties.Resources.textPoC30Imported, importPoC30.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 importPoC30 = new List(); - - try - { - do - { - while (reader.Read()) - { - if (reader.FieldCount < 3) - { - throw new InvalidDataException("Sheet must have at least 3 Columns of data"); - } - PortOfCallLast30Days poc30 = new PortOfCallLast30Days(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) poc30.PortOfCallLast30DaysLocode = reader.GetString(0); - if (!reader.IsDBNull(1)) poc30.PortOfCallLast30DaysDateOfDeparture = reader.GetDateTime(1); - string boolString = ""; - if (!reader.IsDBNull(2)) boolString = reader.GetString(2); - poc30.PortOfCallLast30DaysCrewMembersJoined = (boolString.Equals("y", StringComparison.OrdinalIgnoreCase) || (boolString.Equals("yes", StringComparison.OrdinalIgnoreCase)) || - (boolString.Equals("j", StringComparison.OrdinalIgnoreCase))); - - if(reader.FieldCount > 3) - { - string allNewCrew = reader.GetString(3)?.Trim(); - if(!allNewCrew.IsNullOrEmpty()) - { - string[] crewNames = allNewCrew.Split(',', ';'); - for(int i=0;i 0) - { - this.dataGridPortOfCallLast30Days.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.MDH); - MessageBox.Show(String.Format(Properties.Resources.textPoC30Imported, importPoC30.Count), Properties.Resources.textCaptionInformation, - MessageBoxButton.OK, MessageBoxImage.Information); - } - } - - stream.Close(); } } diff --git a/ENI2/DetailViewControls/SecurityDetailControl.xaml.cs b/ENI2/DetailViewControls/SecurityDetailControl.xaml.cs index 28e45fb4..aa86ade9 100644 --- a/ENI2/DetailViewControls/SecurityDetailControl.xaml.cs +++ b/ENI2/DetailViewControls/SecurityDetailControl.xaml.cs @@ -2,18 +2,17 @@ // Description: SEC Meldung Bearbeitungsseite // -using System.Windows; -using System.Windows.Controls; +using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Util; -using bsmd.database; - -using System.Windows.Data; -using System; using Microsoft.Win32; -using System.IO; -using ExcelDataReader; +using System; using System.Collections.Generic; +using System.IO; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; namespace ENI2.DetailViewControls { @@ -345,76 +344,73 @@ namespace ENI2.DetailViewControls 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(); // Get all rows with data + + List importL10C = new List(); + + int cnt = 0; + foreach (var row in rows) + { + if (cnt >= 10) break; // Maximum 10 rows + + if (worksheet.RangeUsed().ColumnCount() < 8) + { + throw new InvalidDataException("Sheet must have 8 Columns of data"); + } + + LastTenPortFacilitiesCalled l10c = new LastTenPortFacilitiesCalled(); + + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) l10c.PortFacilityPortName = row.Cell(1).GetString(); + if (!row.Cell(3).IsEmpty()) l10c.PortFacilityPortCountry = row.Cell(3).GetString(); + if (!row.Cell(4).IsEmpty()) l10c.PortFacilityPortLoCode = row.Cell(4).GetString(); + + object o = null; + if (!row.Cell(5).IsEmpty()) o = row.Cell(5).Value; + if (o != null) l10c.PortFacilityDateOfArrival = Convert.ToDateTime(o); + + if (!row.Cell(6).IsEmpty()) o = row.Cell(6).Value; + if (o != null) l10c.PortFacilityDateOfDeparture = Convert.ToDateTime(o); + + if (!row.Cell(7).IsEmpty()) o = row.Cell(7).Value; + if (o != null) l10c.PortFacilityShipSecurityLevel = Convert.ToByte(o); + + if (!row.Cell(8).IsEmpty()) o = row.Cell(8).Value; + int gisis = Convert.ToInt32(o); + if (!row.Cell(8).IsEmpty()) l10c.PortFacilityGISISCode = gisis.ToString().PadLeft(4, '0'); + + if (!row.Cell(9).IsEmpty()) l10c.PortFacilitySecurityMattersToReport = row.Cell(9).GetString(); + if (l10c.PortFacilitySecurityMattersToReport?.Equals("nil", StringComparison.CurrentCultureIgnoreCase) == true) + l10c.PortFacilitySecurityMattersToReport = null; + + if (!row.Cell(10).IsEmpty()) l10c.PortFacilityGISISCodeLocode = row.Cell(10).GetString(); + + l10c.SEC = this._sec; + l10c.IsDirty = true; + l10c.Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(this._sec.LastTenPortFacilitesCalled); + this._sec.LastTenPortFacilitesCalled.Add(l10c); + importL10C.Add(l10c); + cnt++; + } + + if (importL10C.Count > 0) + { + this.dataGridLast10PortFacilities.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.SEC); + MessageBox.Show(String.Format(Properties.Resources.textL10PImported, importL10C.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 importL10C = new List(); - - try - { - do - { - int cnt = 0; - while (reader.Read() && (cnt < 10)) - { - if (((IExcelDataReader)reader).FieldCount < 8) - { - throw new InvalidDataException("Sheet must have 8 Columns of data"); - } - LastTenPortFacilitiesCalled l10c = new LastTenPortFacilitiesCalled(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) l10c.PortFacilityPortName = reader.GetString(0); - if (!reader.IsDBNull(2)) l10c.PortFacilityPortCountry = reader.GetString(2); - if (!reader.IsDBNull(3)) l10c.PortFacilityPortLoCode = reader.GetString(3); - object o = null; - if (!reader.IsDBNull(4)) o = reader.GetValue(4); - if(o != null) l10c.PortFacilityDateOfArrival = Convert.ToDateTime(o); - if (!reader.IsDBNull(5)) o = reader.GetValue(5); - if (o != null) l10c.PortFacilityDateOfDeparture = Convert.ToDateTime(o); - // if (!reader.IsDBNull(4)) l10c.PortFacilityDateOfArrival = reader.GetDateTime(4); - // if (!reader.IsDBNull(5)) l10c.PortFacilityDateOfDeparture = reader.GetDateTime(5); - if (!reader.IsDBNull(6)) o = reader.GetValue(6); - if (o != null) l10c.PortFacilityShipSecurityLevel = Convert.ToByte(o); - if (!reader.IsDBNull(7)) o = reader.GetValue(7); - int gisis = Convert.ToInt32(o); - if (!reader.IsDBNull(7)) l10c.PortFacilityGISISCode = gisis.ToString().PadLeft(4, '0'); - if (!reader.IsDBNull(8)) l10c.PortFacilitySecurityMattersToReport = reader.GetString(8); - if (l10c.PortFacilitySecurityMattersToReport.Equals("nil", StringComparison.CurrentCultureIgnoreCase)) - l10c.PortFacilitySecurityMattersToReport = null; - if (!reader.IsDBNull(9)) l10c.PortFacilityGISISCodeLocode = reader.GetString(9); - l10c.SEC = this._sec; - l10c.IsDirty = true; - l10c.Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(this._sec.LastTenPortFacilitesCalled); - this._sec.LastTenPortFacilitesCalled.Add(l10c); - importL10C.Add(l10c); - cnt++; - } - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (importL10C.Count > 0) - { - this.dataGridLast10PortFacilities.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.SEC); - MessageBox.Show(String.Format(Properties.Resources.textL10PImported, importL10C.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - - stream.Close(); } } diff --git a/ENI2/DetailViewControls/WasteDetailControl.xaml.cs b/ENI2/DetailViewControls/WasteDetailControl.xaml.cs index 6ba99603..d3fe3ea7 100644 --- a/ENI2/DetailViewControls/WasteDetailControl.xaml.cs +++ b/ENI2/DetailViewControls/WasteDetailControl.xaml.cs @@ -2,17 +2,17 @@ // Description: Detailansicht Müllmeldung // -using System.Linq; -using System.Collections.Generic; -using System.Windows; -using System.Windows.Controls; +using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Util; -using bsmd.database; -using ExcelDataReader; -using System.IO; -using System; using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Windows; +using System.Windows.Controls; namespace ENI2.DetailViewControls { @@ -483,100 +483,82 @@ namespace ENI2.DetailViewControls 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; + 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(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(); - } } diff --git a/ENI2/ENI2.csproj b/ENI2/ENI2.csproj index 165b7209..31277d91 100644 --- a/ENI2/ENI2.csproj +++ b/ENI2/ENI2.csproj @@ -146,9 +146,6 @@ packages\DocumentFormat.OpenXml.Framework.3.1.1\lib\net46\DocumentFormat.OpenXml.Framework.dll - - packages\ExcelDataReader.3.8.0\lib\net462\ExcelDataReader.dll - packages\ExcelNumberFormat.1.1.0\lib\net20\ExcelNumberFormat.dll diff --git a/ENI2/SheetDisplayControls/CrewDepartureControl.xaml.cs b/ENI2/SheetDisplayControls/CrewDepartureControl.xaml.cs index 4719f84e..0f186fbd 100644 --- a/ENI2/SheetDisplayControls/CrewDepartureControl.xaml.cs +++ b/ENI2/SheetDisplayControls/CrewDepartureControl.xaml.cs @@ -3,9 +3,9 @@ // using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Util; -using ExcelDataReader; using Microsoft.Win32; using System; using System.Collections.Generic; @@ -126,72 +126,66 @@ namespace ENI2.SheetDisplayControls }; 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(1); // Skip header row if present + + List importCrew = new List(); + + foreach (var row in rows) + { + if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break; + + // Check if we have at least 13 columns + if (worksheet.RangeUsed().ColumnCount() < 13) + { + throw new InvalidDataException("Sheet must have 13 columns of data"); + } + + CREWD crew = new CREWD(); + crew.IsDeparture = true; + + // Check if first two cells are empty + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) crew.CrewMemberLastName = row.Cell(1).GetString().Clean(); + if (crew.CrewMemberLastName?.Equals("Family Name") == true || string.IsNullOrWhiteSpace(crew.CrewMemberLastName)) continue; + + if (!row.Cell(2).IsEmpty()) crew.CrewMemberFirstName = row.Cell(2).GetString().Clean(); + if (!row.Cell(3).IsEmpty()) crew.CrewMemberGender = GlobalStructures.ParseGender(row.Cell(3).GetString()); + if (!row.Cell(4).IsEmpty()) crew.CrewMemberDuty = row.Cell(4).GetString().Clean(); + if (!row.Cell(5).IsEmpty()) crew.CrewMemberNationality = row.Cell(5).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(6).IsEmpty()) crew.CrewMemberPlaceOfBirth = row.Cell(6).GetString().Clean(); + if (!row.Cell(7).IsEmpty()) crew.CrewMemberCountryOfBirth = row.Cell(7).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(8).IsEmpty()) crew.CrewMemberDateOfBirth = row.Cell(8).GetDateTime(); + if (!row.Cell(9).IsEmpty()) crew.CrewMemberIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(row.Cell(9).GetString()); + if (!row.Cell(10).IsEmpty()) crew.CrewMemberIdentityDocumentId = row.Cell(10).GetString().Clean(); + if (!row.Cell(11).IsEmpty()) crew.CrewMemberIdentityDocumentIssuingState = row.Cell(11).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(12).IsEmpty()) crew.CrewMemberIdentityDocumentExpiryDate = row.Cell(12).GetDateTime(); + if (!row.Cell(13).IsEmpty()) crew.CrewMemberVisaNumber = row.Cell(13).GetString().Clean(); + + crew.MessageHeader = this._crewdMessage; + crew.IsDirty = true; + crew.Identifier = CREWD.GetNewIdentifier(this._crewdMessage.Elements); + this._crewdMessage.Elements.Add(crew); + importCrew.Add(crew); + } + + if (importCrew.Count > 0) + { + this.dataGridCrewListDeparture.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.CREWD); + MessageBox.Show(String.Format(Properties.Resources.textCrewImported, importCrew.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 (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) - { - List importCrew = new List(); - - try - { - do - { - while (reader.Read()) - { - if (reader.FieldCount < 13) - { - throw new InvalidDataException("Sheet must have 13 columns of data"); - } - CREWD crew = new CREWD(); - crew.IsDeparture = true; - - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) crew.CrewMemberLastName = reader.GetString(0); - if (crew.CrewMemberLastName.Equals("Family Name") || (crew.CrewMemberLastName.Trim().Length == 0)) continue; - if (!reader.IsDBNull(1)) crew.CrewMemberFirstName = reader.GetString(1); - if (!reader.IsDBNull(2)) crew.CrewMemberGender = GlobalStructures.ParseGender(reader.GetString(2)); - if (!reader.IsDBNull(3)) crew.CrewMemberDuty = reader.GetString(3).Clean(); - if (!reader.IsDBNull(4)) crew.CrewMemberNationality = reader.GetString(4).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(5)) crew.CrewMemberPlaceOfBirth = reader.GetString(5); - if (!reader.IsDBNull(6)) crew.CrewMemberCountryOfBirth = reader.GetString(6).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(7)) crew.CrewMemberDateOfBirth = reader.GetDateTime(7); - if (!reader.IsDBNull(8)) crew.CrewMemberIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(reader.GetString(8)); - if (!reader.IsDBNull(9)) crew.CrewMemberIdentityDocumentId = reader.ReadAsString(9); - if (!reader.IsDBNull(10)) crew.CrewMemberIdentityDocumentIssuingState = reader.GetString(10).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(11)) crew.CrewMemberIdentityDocumentExpiryDate = reader.GetDateTime(11); - if (!reader.IsDBNull(12)) crew.CrewMemberVisaNumber = reader.ReadAsString(12); - - crew.MessageHeader = this._crewdMessage; - crew.IsDirty = true; - crew.Identifier = CREWD.GetNewIdentifier(this._crewdMessage.Elements); - this._crewdMessage.Elements.Add(crew); - importCrew.Add(crew); - } - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (importCrew.Count > 0) - { - this.dataGridCrewListDeparture.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.CREWD); - MessageBox.Show(String.Format(Properties.Resources.textCrewImported, importCrew.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - - stream.Close(); } } diff --git a/ENI2/SheetDisplayControls/CrewPreArrivalControl.xaml.cs b/ENI2/SheetDisplayControls/CrewPreArrivalControl.xaml.cs index 8678b30c..0bf4e98f 100644 --- a/ENI2/SheetDisplayControls/CrewPreArrivalControl.xaml.cs +++ b/ENI2/SheetDisplayControls/CrewPreArrivalControl.xaml.cs @@ -3,9 +3,9 @@ // using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Util; -using ExcelDataReader; using Microsoft.Win32; using System; using System.Collections.Generic; @@ -166,70 +166,65 @@ namespace ENI2.SheetDisplayControls }; 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(1); // Skip header row if present + + List importCrew = new List(); + + foreach (var row in rows) + { + if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break; + + // Check if we have at least 13 columns + if (worksheet.RangeUsed().ColumnCount() < 13) + { + throw new InvalidDataException("Sheet must have 13 columns of data"); + } + + CREW crew = new CREW(); + + // Check if first two cells are empty + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) crew.CrewMemberLastName = row.Cell(1).GetString().Clean(); + if (crew.CrewMemberLastName?.Equals("Family Name") == true || string.IsNullOrWhiteSpace(crew.CrewMemberLastName)) continue; + + if (!row.Cell(2).IsEmpty()) crew.CrewMemberFirstName = row.Cell(2).GetString().Clean(); + if (!row.Cell(3).IsEmpty()) crew.CrewMemberGender = GlobalStructures.ParseGender(row.Cell(3).GetString()); + if (!row.Cell(4).IsEmpty()) crew.CrewMemberDuty = row.Cell(4).GetString().Clean(); + if (!row.Cell(5).IsEmpty()) crew.CrewMemberNationality = row.Cell(5).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(6).IsEmpty()) crew.CrewMemberPlaceOfBirth = row.Cell(6).GetString().Clean(); + if (!row.Cell(7).IsEmpty()) crew.CrewMemberCountryOfBirth = row.Cell(7).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(8).IsEmpty()) crew.CrewMemberDateOfBirth = row.Cell(8).GetDateTime(); + if (!row.Cell(9).IsEmpty()) crew.CrewMemberIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(row.Cell(9).GetString()); + if (!row.Cell(10).IsEmpty()) crew.CrewMemberIdentityDocumentId = row.Cell(10).GetString().Clean(); + if (!row.Cell(11).IsEmpty()) crew.CrewMemberIdentityDocumentIssuingState = row.Cell(11).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(12).IsEmpty()) crew.CrewMemberIdentityDocumentExpiryDate = row.Cell(12).GetDateTime(); + if (!row.Cell(13).IsEmpty()) crew.CrewMemberVisaNumber = row.Cell(13).GetString().Clean(); + + crew.MessageHeader = this._crewaMessage; + crew.IsDirty = true; + crew.Identifier = CREW.GetNewIdentifier(this._crewaMessage.Elements); + this._crewaMessage.Elements.Add(crew); + importCrew.Add(crew); + } + + if (importCrew.Count > 0) + { + this.dataGridCrewList.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.CREWA); + MessageBox.Show(String.Format(Properties.Resources.textCrewImported, importCrew.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 (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) - { - List importCrew = new List(); - - try - { - do - { - while (reader.Read()) - { - if (reader.FieldCount < 13) - { - throw new InvalidDataException("Sheet must have 13 columns of data"); - } - CREW crew = new CREW(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) crew.CrewMemberLastName = reader.GetString(0).Clean(); - if (crew.CrewMemberLastName.Equals("Family Name") || (crew.CrewMemberLastName.Trim().Length == 0)) continue; - if (!reader.IsDBNull(1)) crew.CrewMemberFirstName = reader.GetString(1).Clean(); - if (!reader.IsDBNull(2)) crew.CrewMemberGender = GlobalStructures.ParseGender(reader.GetString(2)); - if (!reader.IsDBNull(3)) crew.CrewMemberDuty = reader.GetString(3).Clean(); - if (!reader.IsDBNull(4)) crew.CrewMemberNationality = reader.GetString(4).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(5)) crew.CrewMemberPlaceOfBirth = reader.GetString(5).Clean(); - if (!reader.IsDBNull(6)) crew.CrewMemberCountryOfBirth = reader.GetString(6).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(7)) crew.CrewMemberDateOfBirth = reader.GetDateTime(7); - if (!reader.IsDBNull(8)) crew.CrewMemberIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(reader.GetString(8)); - if (!reader.IsDBNull(9)) crew.CrewMemberIdentityDocumentId = reader.ReadAsString(9).Clean(); - if (!reader.IsDBNull(10)) crew.CrewMemberIdentityDocumentIssuingState = reader.GetString(10).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(11)) crew.CrewMemberIdentityDocumentExpiryDate = reader.GetDateTime(11); - if (!reader.IsDBNull(12)) crew.CrewMemberVisaNumber = reader.ReadAsString(12).Clean(); - - crew.MessageHeader = this._crewaMessage; - crew.IsDirty = true; - crew.Identifier = CREW.GetNewIdentifier(this._crewaMessage.Elements); - this._crewaMessage.Elements.Add(crew); - importCrew.Add(crew); - } - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (importCrew.Count > 0) - { - this.dataGridCrewList.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.CREWA); - MessageBox.Show(String.Format(Properties.Resources.textCrewImported, importCrew.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - - stream.Close(); } } diff --git a/ENI2/SheetDisplayControls/PassengerDepartureControl.xaml.cs b/ENI2/SheetDisplayControls/PassengerDepartureControl.xaml.cs index 24ca2cfa..8e93f552 100644 --- a/ENI2/SheetDisplayControls/PassengerDepartureControl.xaml.cs +++ b/ENI2/SheetDisplayControls/PassengerDepartureControl.xaml.cs @@ -3,10 +3,10 @@ // using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Locode; using ENI2.Util; -using ExcelDataReader; using Microsoft.Win32; using System; using System.Collections.Generic; @@ -144,78 +144,71 @@ namespace ENI2.SheetDisplayControls }; 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(1); // Skip header row if present + + List importPassenger = new List(); + + foreach (var row in rows) + { + if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break; + + if (worksheet.RangeUsed().ColumnCount() < 17) + { + throw new InvalidDataException("Sheet must have 17 columns of data"); + } + + PASD pas = new PASD(); + + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) pas.PassengerLastName = row.Cell(1).GetString().Clean(); + if (pas.PassengerLastName?.Equals("Family Name") == true || string.IsNullOrWhiteSpace(pas.PassengerLastName)) continue; + + if (!row.Cell(2).IsEmpty()) pas.PassengerFirstName = row.Cell(2).GetString().Clean(); + if (!row.Cell(3).IsEmpty()) pas.PassengerGender = GlobalStructures.ParseGender(row.Cell(3).GetString()); + if (!row.Cell(4).IsEmpty()) pas.PassengerPortOfEmbarkation = row.Cell(4).GetString().Clean(); + if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfEmbarkation) == null) + pas.PassengerPortOfEmbarkation = null; + if (!row.Cell(5).IsEmpty()) pas.PassengerPortOfDisembarkation = row.Cell(5).GetString().Clean(); + if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfDisembarkation) == null) + pas.PassengerPortOfDisembarkation = null; + if (!row.Cell(6).IsEmpty()) pas.PassengerInTransit = GlobalStructures.ReadBoolean(row.Cell(6).GetString()); + if (!row.Cell(7).IsEmpty()) pas.PassengerNationality = row.Cell(7).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(8).IsEmpty()) pas.PassengerPlaceOfBirth = row.Cell(8).GetString().Clean(); + if (!row.Cell(9).IsEmpty()) pas.PassengerCountryOfBirth = row.Cell(9).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(10).IsEmpty()) pas.PassengerDateOfBirth = row.Cell(10).GetDateTime(); + if (!row.Cell(11).IsEmpty()) pas.PassengerIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(row.Cell(11).GetString()); + if (!row.Cell(12).IsEmpty()) pas.PassengerIdentityDocumentId = row.Cell(12).GetString().Clean(); + if (!row.Cell(13).IsEmpty()) pas.PassengerIdentityDocumentIssuingState = row.Cell(13).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(14).IsEmpty()) pas.PassengerIdentityDocumentExpiryDate = row.Cell(14).GetDateTime(); + if (!row.Cell(15).IsEmpty()) pas.PassengerVisaNumber = row.Cell(15).GetString().Clean(); + if (!row.Cell(16).IsEmpty()) pas.EmergencyCare = row.Cell(16).GetString().Clean(); + if (!row.Cell(17).IsEmpty()) pas.EmergencyContactNumber = row.Cell(17).GetString().Clean(); + + pas.MessageHeader = this._pasdMessage; + pas.IsDirty = true; + pas.Identifier = PASD.GetNewIdentifier(this._pasdMessage.Elements); + this._pasdMessage.Elements.Add(pas); + importPassenger.Add(pas); + } + + if (importPassenger.Count > 0) + { + this.dataGridPassengerListDeparture.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.PASD); + MessageBox.Show(String.Format(Properties.Resources.textPassengerImported, importPassenger.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 importPassenger = new List(); - - try - { - do - { - while (reader.Read()) - { - if (((IExcelDataReader)reader).FieldCount < 17) - { - throw new InvalidDataException("Sheet must have 17 columns of data"); - } - - PASD pas = new PASD(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) pas.PassengerLastName = reader.GetValue(0).ToString(); - if (pas.PassengerLastName.Equals("Family Name") || (pas.PassengerLastName.Trim().Length == 0)) continue; - if (!reader.IsDBNull(1)) pas.PassengerFirstName = reader.GetValue(1).ToString(); - if (!reader.IsDBNull(2)) pas.PassengerGender = GlobalStructures.ParseGender(reader.GetString(2)); - if (!reader.IsDBNull(3)) pas.PassengerPortOfEmbarkation = reader.GetString(3); - if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfEmbarkation) == null) - pas.PassengerPortOfEmbarkation = null; - if (!reader.IsDBNull(4)) pas.PassengerPortOfDisembarkation = reader.GetString(4); - if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfDisembarkation) == null) - pas.PassengerPortOfDisembarkation = null; - if (!reader.IsDBNull(5)) pas.PassengerInTransit = GlobalStructures.ReadBoolean(reader.GetString(5)); - if (!reader.IsDBNull(6)) pas.PassengerNationality = reader.GetString(6).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(7)) pas.PassengerPlaceOfBirth = reader.GetString(7); - if (!reader.IsDBNull(8)) pas.PassengerCountryOfBirth = reader.GetString(8).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(9)) pas.PassengerDateOfBirth = reader.GetDateTime(9); - if (!reader.IsDBNull(10)) pas.PassengerIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(reader.GetString(10)); - if (!reader.IsDBNull(11)) pas.PassengerIdentityDocumentId = reader.ReadAsString( 11); - if (!reader.IsDBNull(12)) pas.PassengerIdentityDocumentIssuingState = reader.GetString(12).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(13)) pas.PassengerIdentityDocumentExpiryDate = reader.GetDateTime(13); - if (!reader.IsDBNull(14)) pas.PassengerVisaNumber = reader.ReadAsString(14); - if (!reader.IsDBNull(15)) pas.EmergencyCare = reader.GetString(15); - if (!reader.IsDBNull(16)) pas.EmergencyContactNumber = reader.ReadAsString(16); - - pas.MessageHeader = this._pasMessage; - pas.IsDirty = true; - pas.Identifier = PASD.GetNewIdentifier(this._pasdMessage.Elements); - this._pasdMessage.Elements.Add(pas); - importPassenger.Add(pas); - } - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (importPassenger.Count > 0) - { - this.dataGridPassengerListDeparture.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.PASD); - MessageBox.Show(String.Format(Properties.Resources.textPassengerImported, importPassenger.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - stream.Close(); } } diff --git a/ENI2/SheetDisplayControls/PassengerPreArrivalControl.xaml.cs b/ENI2/SheetDisplayControls/PassengerPreArrivalControl.xaml.cs index 5350b8c3..8beaa753 100644 --- a/ENI2/SheetDisplayControls/PassengerPreArrivalControl.xaml.cs +++ b/ENI2/SheetDisplayControls/PassengerPreArrivalControl.xaml.cs @@ -4,10 +4,10 @@ // using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Locode; using ENI2.Util; -using ExcelDataReader; using Microsoft.Win32; using System; using System.Collections.Generic; @@ -126,78 +126,71 @@ namespace ENI2.SheetDisplayControls }; 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(1); // Skip header row if present + + List importPassenger = new List(); + + foreach (var row in rows) + { + if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break; + + if (worksheet.RangeUsed().ColumnCount() < 17) + { + throw new InvalidDataException("Sheet must have 17 columns of data"); + } + + PAS pas = new PAS(); + + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) pas.PassengerLastName = row.Cell(1).GetString().Clean(); + if (pas.PassengerLastName?.Equals("Family Name") == true || string.IsNullOrWhiteSpace(pas.PassengerLastName)) continue; + + if (!row.Cell(2).IsEmpty()) pas.PassengerFirstName = row.Cell(2).GetString().Clean(); + if (!row.Cell(3).IsEmpty()) pas.PassengerGender = GlobalStructures.ParseGender(row.Cell(3).GetString()); + if (!row.Cell(4).IsEmpty()) pas.PassengerPortOfEmbarkation = row.Cell(4).GetString().Clean(); + if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfEmbarkation) == null) + pas.PassengerPortOfEmbarkation = null; + if (!row.Cell(5).IsEmpty()) pas.PassengerPortOfDisembarkation = row.Cell(5).GetString().Clean(); + if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfDisembarkation) == null) + pas.PassengerPortOfDisembarkation = null; + if (!row.Cell(6).IsEmpty()) pas.PassengerInTransit = GlobalStructures.ReadBoolean(row.Cell(6).GetString()); + if (!row.Cell(7).IsEmpty()) pas.PassengerNationality = row.Cell(7).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(8).IsEmpty()) pas.PassengerPlaceOfBirth = row.Cell(8).GetString().Clean(); + if (!row.Cell(9).IsEmpty()) pas.PassengerCountryOfBirth = row.Cell(9).GetString().Substring(0, 2).ToUpper().Clean(); + if (!row.Cell(10).IsEmpty()) pas.PassengerDateOfBirth = row.Cell(10).GetDateTime(); + if (!row.Cell(11).IsEmpty()) pas.PassengerIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(row.Cell(11).GetString()); + if (!row.Cell(12).IsEmpty()) pas.PassengerIdentityDocumentId = row.Cell(12).GetString().Clean(); + if (!row.Cell(13).IsEmpty()) pas.PassengerIdentityDocumentIssuingState = row.Cell(13).GetString().Substring(0, 2).ToUpper(); + if (!row.Cell(14).IsEmpty()) pas.PassengerIdentityDocumentExpiryDate = row.Cell(14).GetDateTime(); + if (!row.Cell(15).IsEmpty()) pas.PassengerVisaNumber = row.Cell(15).GetString().Clean(); + if (!row.Cell(16).IsEmpty()) pas.EmergencyCare = row.Cell(16).GetString().Clean(); + if (!row.Cell(17).IsEmpty()) pas.EmergencyContactNumber = row.Cell(17).GetString().Clean(); + + pas.MessageHeader = this._pasMessage; + pas.IsDirty = true; + pas.Identifier = PAS.GetNewIdentifier(this._pasMessage.Elements); + this._pasMessage.Elements.Add(pas); + importPassenger.Add(pas); + } + + if (importPassenger.Count > 0) + { + this.dataGridPassengerList.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.PASA); + MessageBox.Show(String.Format(Properties.Resources.textPassengerImported, importPassenger.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 importPassenger = new List(); - - try - { - do - { - while (reader.Read()) - { - if (((IExcelDataReader)reader).FieldCount < 17) - { - throw new InvalidDataException("Sheet must have 17 columns of data"); - } - - PAS pas = new PAS(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) pas.PassengerLastName = reader.GetValue(0).ToString().Clean(); - if (pas.PassengerLastName.Equals("Family Name") || (pas.PassengerLastName.Trim().Length == 0)) continue; - if (!reader.IsDBNull(1)) pas.PassengerFirstName = reader.GetValue(1).ToString().Clean(); - if (!reader.IsDBNull(2)) pas.PassengerGender = GlobalStructures.ParseGender(reader.GetString(2)); - if (!reader.IsDBNull(3)) pas.PassengerPortOfEmbarkation = reader.GetString(3).Clean(); - if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfEmbarkation) == null) - pas.PassengerPortOfEmbarkation = null; - if (!reader.IsDBNull(4)) pas.PassengerPortOfDisembarkation = reader.GetString(4).Clean(); - if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfDisembarkation) == null) - pas.PassengerPortOfDisembarkation = null; - if (!reader.IsDBNull(5)) pas.PassengerInTransit = GlobalStructures.ReadBoolean(reader.GetString(5)); - if (!reader.IsDBNull(6)) pas.PassengerNationality = reader.GetString(6).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(7)) pas.PassengerPlaceOfBirth = reader.GetString(7).Clean(); - if (!reader.IsDBNull(8)) pas.PassengerCountryOfBirth = reader.GetString(8).Substring(0, 2).ToUpper().Clean(); - if (!reader.IsDBNull(9)) pas.PassengerDateOfBirth = reader.GetDateTime(9); - if (!reader.IsDBNull(10)) pas.PassengerIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(reader.GetString(10)); - if (!reader.IsDBNull(11)) pas.PassengerIdentityDocumentId = reader.ReadAsString(11).Clean(); - if (!reader.IsDBNull(12)) pas.PassengerIdentityDocumentIssuingState = reader.GetString(12).Substring(0, 2).ToUpper(); - if (!reader.IsDBNull(13)) pas.PassengerIdentityDocumentExpiryDate = reader.GetDateTime(13); - if (!reader.IsDBNull(14)) pas.PassengerVisaNumber = reader.ReadAsString(14).Clean(); - if (!reader.IsDBNull(15)) pas.EmergencyCare = reader.GetString(15).Clean(); - if (!reader.IsDBNull(16)) pas.EmergencyContactNumber = reader.ReadAsString(16).Clean(); - - pas.MessageHeader = this._pasMessage; - pas.IsDirty = true; - pas.Identifier = PAS.GetNewIdentifier(this._pasMessage.Elements); - this._pasMessage.Elements.Add(pas); - importPassenger.Add(pas); - } - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (importPassenger.Count > 0) - { - this.dataGridPassengerList.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.PASA); - MessageBox.Show(String.Format(Properties.Resources.textPassengerImported, importPassenger.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - stream.Close(); } } diff --git a/ENI2/SheetDisplayControls/VoyageControl.xaml.cs b/ENI2/SheetDisplayControls/VoyageControl.xaml.cs index be9d4eec..2c773019 100644 --- a/ENI2/SheetDisplayControls/VoyageControl.xaml.cs +++ b/ENI2/SheetDisplayControls/VoyageControl.xaml.cs @@ -2,17 +2,16 @@ // Description: Display control of formsheet Tab 1. Voyage // -using System.Windows; - using bsmd.database; +using ClosedXML.Excel; using ENI2.EditControls; -using System.Windows.Controls; -using System; using ENI2.Util; -using System.Collections.Generic; -using ExcelDataReader; using Microsoft.Win32; +using System; +using System.Collections.Generic; using System.IO; +using System.Windows; +using System.Windows.Controls; namespace ENI2.SheetDisplayControls @@ -308,76 +307,73 @@ namespace ENI2.SheetDisplayControls 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(); // Get all rows with data + + List importL10C = new List(); + + int cnt = 0; + foreach (var row in rows) + { + if (cnt >= 10) break; // Maximum 10 rows + + if (worksheet.RangeUsed().ColumnCount() < 8) + { + throw new InvalidDataException("Sheet must have 8 Columns of data"); + } + + LastTenPortFacilitiesCalled l10c = new LastTenPortFacilitiesCalled(); + + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) l10c.PortFacilityPortName = row.Cell(1).GetString(); + if (!row.Cell(3).IsEmpty()) l10c.PortFacilityPortCountry = row.Cell(3).GetString(); + if (!row.Cell(4).IsEmpty()) l10c.PortFacilityPortLoCode = row.Cell(4).GetString(); + + object o = null; + if (!row.Cell(5).IsEmpty()) o = row.Cell(5).Value; + if (o != null) l10c.PortFacilityDateOfArrival = Convert.ToDateTime(o); + + if (!row.Cell(6).IsEmpty()) o = row.Cell(6).Value; + if (o != null) l10c.PortFacilityDateOfDeparture = Convert.ToDateTime(o); + + if (!row.Cell(7).IsEmpty()) o = row.Cell(7).Value; + if (o != null) l10c.PortFacilityShipSecurityLevel = Convert.ToByte(o); + + if (!row.Cell(8).IsEmpty()) o = row.Cell(8).Value; + int gisis = Convert.ToInt32(o); + if (!row.Cell(8).IsEmpty()) l10c.PortFacilityGISISCode = gisis.ToString().PadLeft(4, '0'); + + if (!row.Cell(9).IsEmpty()) l10c.PortFacilitySecurityMattersToReport = row.Cell(9).GetString(); + if (l10c.PortFacilitySecurityMattersToReport?.Equals("nil", StringComparison.CurrentCultureIgnoreCase) == true) + l10c.PortFacilitySecurityMattersToReport = null; + + if (!row.Cell(10).IsEmpty()) l10c.PortFacilityGISISCodeLocode = row.Cell(10).GetString(); + + l10c.SEC = this._sec; + l10c.IsDirty = true; + l10c.Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(this._sec.LastTenPortFacilitesCalled); + this._sec.LastTenPortFacilitesCalled.Add(l10c); + importL10C.Add(l10c); + cnt++; + } + + if (importL10C.Count > 0) + { + this.dataGridLast10PortFacilities.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.SEC); + MessageBox.Show(String.Format(Properties.Resources.textL10PImported, importL10C.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 importL10C = new List(); - - try - { - do - { - int cnt = 0; - while (reader.Read() && (cnt < 10)) - { - if (((IExcelDataReader)reader).FieldCount < 8) - { - throw new InvalidDataException("Sheet must have 8 Columns of data"); - } - LastTenPortFacilitiesCalled l10c = new LastTenPortFacilitiesCalled(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) l10c.PortFacilityPortName = reader.GetString(0); - if (!reader.IsDBNull(2)) l10c.PortFacilityPortCountry = reader.GetString(2); - if (!reader.IsDBNull(3)) l10c.PortFacilityPortLoCode = reader.GetString(3); - object o = null; - if (!reader.IsDBNull(4)) o = reader.GetValue(4); - if (o != null) l10c.PortFacilityDateOfArrival = Convert.ToDateTime(o); - if (!reader.IsDBNull(5)) o = reader.GetValue(5); - if (o != null) l10c.PortFacilityDateOfDeparture = Convert.ToDateTime(o); - // if (!reader.IsDBNull(4)) l10c.PortFacilityDateOfArrival = reader.GetDateTime(4); - // if (!reader.IsDBNull(5)) l10c.PortFacilityDateOfDeparture = reader.GetDateTime(5); - if (!reader.IsDBNull(6)) o = reader.GetValue(6); - if (o != null) l10c.PortFacilityShipSecurityLevel = Convert.ToByte(o); - if (!reader.IsDBNull(7)) o = reader.GetValue(7); - int gisis = Convert.ToInt32(o); - if (!reader.IsDBNull(7)) l10c.PortFacilityGISISCode = gisis.ToString().PadLeft(4, '0'); - if (!reader.IsDBNull(8)) l10c.PortFacilitySecurityMattersToReport = reader.GetString(8); - if (l10c.PortFacilitySecurityMattersToReport.Equals("nil", StringComparison.CurrentCultureIgnoreCase)) - l10c.PortFacilitySecurityMattersToReport = null; - if (!reader.IsDBNull(9)) l10c.PortFacilityGISISCodeLocode = reader.GetString(9); - l10c.SEC = this._sec; - l10c.IsDirty = true; - l10c.Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(this._sec.LastTenPortFacilitesCalled); - this._sec.LastTenPortFacilitesCalled.Add(l10c); - importL10C.Add(l10c); - cnt++; - } - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (importL10C.Count > 0) - { - this.dataGridLast10PortFacilities.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.SEC); - MessageBox.Show(String.Format(Properties.Resources.textL10PImported, importL10C.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - - stream.Close(); } } @@ -453,80 +449,73 @@ namespace ENI2.SheetDisplayControls }; 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(); // Get all rows with data + + List importPoC30 = new List(); + + foreach (var row in rows) + { + if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break; + + if (worksheet.RangeUsed().ColumnCount() < 3) + { + throw new InvalidDataException("Sheet must have at least 3 Columns of data"); + } + + PortOfCallLast30Days poc30 = new PortOfCallLast30Days(); + + if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; + + if (!row.Cell(1).IsEmpty()) poc30.PortOfCallLast30DaysLocode = row.Cell(1).GetString(); + if (!row.Cell(2).IsEmpty()) poc30.PortOfCallLast30DaysDateOfDeparture = row.Cell(2).GetDateTime(); + + string boolString = ""; + if (!row.Cell(3).IsEmpty()) boolString = row.Cell(3).GetString(); + poc30.PortOfCallLast30DaysCrewMembersJoined = (boolString.Equals("y", StringComparison.OrdinalIgnoreCase) || + (boolString.Equals("yes", StringComparison.OrdinalIgnoreCase)) || + (boolString.Equals("j", StringComparison.OrdinalIgnoreCase))); + + if (worksheet.RangeUsed().ColumnCount() > 3) + { + string allNewCrew = row.Cell(4).IsEmpty() ? null : row.Cell(4).GetString()?.Trim(); + if (!allNewCrew.IsNullOrEmpty()) + { + string[] crewNames = allNewCrew.Split(',', ';'); + for (int i = 0; i < crewNames.Length; i++) + { + string crewName = crewNames[i].Trim(); + if (crewName.IsNullOrEmpty()) continue; + PortOfCallLast30DaysCrewJoinedShip poc30Crew = new PortOfCallLast30DaysCrewJoinedShip(); + poc30Crew.PortOfCallLast30DaysCrewJoinedShipName = crewName; + poc30Crew.PortOfCallLast30Days = poc30; + poc30.CrewJoinedShip.Add(poc30Crew); + } + } + } + + poc30.MDH = this._mdh; + this._mdh.PortOfCallLast30Days.Add(poc30); + importPoC30.Add(poc30); + } + + if (importPoC30.Count > 0) + { + this.dataGridPortOfCallLast30Days.Items.Refresh(); + this.SublistElementChanged(Message.NotificationClass.MDH); + MessageBox.Show(String.Format(Properties.Resources.textPoC30Imported, importPoC30.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 importPoC30 = new List(); - - try - { - do - { - while (reader.Read()) - { - if (reader.FieldCount < 3) - { - throw new InvalidDataException("Sheet must have at least 3 Columns of data"); - } - PortOfCallLast30Days poc30 = new PortOfCallLast30Days(); - if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; - if (!reader.IsDBNull(0)) poc30.PortOfCallLast30DaysLocode = reader.GetString(0); - if (!reader.IsDBNull(1)) poc30.PortOfCallLast30DaysDateOfDeparture = reader.GetDateTime(1); - string boolString = ""; - if (!reader.IsDBNull(2)) boolString = reader.GetString(2); - poc30.PortOfCallLast30DaysCrewMembersJoined = (boolString.Equals("y", StringComparison.OrdinalIgnoreCase) || (boolString.Equals("yes", StringComparison.OrdinalIgnoreCase)) || - (boolString.Equals("j", StringComparison.OrdinalIgnoreCase))); - - if (reader.FieldCount > 3) - { - string allNewCrew = reader.GetString(3)?.Trim(); - if (!allNewCrew.IsNullOrEmpty()) - { - string[] crewNames = allNewCrew.Split(',', ';'); - for (int i = 0; i < crewNames.Length; i++) - { - string crewName = crewNames[i].Trim(); - if (crewName.IsNullOrEmpty()) continue; - PortOfCallLast30DaysCrewJoinedShip poc30Crew = new PortOfCallLast30DaysCrewJoinedShip(); - poc30Crew.PortOfCallLast30DaysCrewJoinedShipName = crewName; - poc30Crew.PortOfCallLast30Days = poc30; - poc30.CrewJoinedShip.Add(poc30Crew); - - } - } - } - - poc30.MDH = this._mdh; - this._mdh.PortOfCallLast30Days.Add(poc30); - importPoC30.Add(poc30); - } - } while (reader.NextResult()); - } - catch (Exception ex) - { - MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); - } - - if (importPoC30.Count > 0) - { - this.dataGridPortOfCallLast30Days.Items.Refresh(); - this.SublistElementChanged(Message.NotificationClass.MDH); - MessageBox.Show(String.Format(Properties.Resources.textPoC30Imported, importPoC30.Count), Properties.Resources.textCaptionInformation, - MessageBoxButton.OK, MessageBoxImage.Information); - } - } - - stream.Close(); } } diff --git a/ENI2/Util/Extensions.cs b/ENI2/Util/Extensions.cs index 79afbe12..606eaaa5 100644 --- a/ENI2/Util/Extensions.cs +++ b/ENI2/Util/Extensions.cs @@ -3,7 +3,7 @@ // using bsmd.database; -using ExcelDataReader; + using System; namespace ENI2.Util @@ -11,20 +11,6 @@ namespace ENI2.Util internal static class Extensions { - public static string ReadAsString(this IExcelDataReader reader, int index) - { - if (index >= reader.FieldCount) return null; - Type fieldType = reader.GetFieldType(index); - if (fieldType == null) return null; - if (fieldType == typeof(string)) - return reader.GetString(index).Clean(); - if (fieldType == typeof(DateTime)) - return reader.GetDateTime(index).ToString(); - if (fieldType == typeof(int)) - return reader.GetInt32(index).ToString(); - if (fieldType == typeof(double)) - return ((int)reader.GetDouble(index)).ToString(); - return null; - } + } } diff --git a/ENI2/packages.config b/ENI2/packages.config index a7fde4a9..4e5e7372 100644 --- a/ENI2/packages.config +++ b/ENI2/packages.config @@ -4,7 +4,6 @@ -