diff --git a/ENI2/Controls/HazardMaterialControl.xaml b/ENI2/Controls/HazardMaterialControl.xaml
index 63179853..6a234d88 100644
--- a/ENI2/Controls/HazardMaterialControl.xaml
+++ b/ENI2/Controls/HazardMaterialControl.xaml
@@ -21,12 +21,14 @@
+
+
diff --git a/ENI2/Controls/HazardMaterialControl.xaml.cs b/ENI2/Controls/HazardMaterialControl.xaml.cs
index ef3e890d..b632a13e 100644
--- a/ENI2/Controls/HazardMaterialControl.xaml.cs
+++ b/ENI2/Controls/HazardMaterialControl.xaml.cs
@@ -1,4 +1,5 @@
using bsmd.database;
+using ENI2.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@@ -56,6 +57,15 @@ namespace ENI2.Controls
AddNewTemplate();
}
+ private void buttonImport_Click(object sender, RoutedEventArgs e)
+ {
+ var imported = ExcelLocalImportHelper.ImportHazardMaterials();
+ foreach (var item in imported)
+ {
+ HAZPosTemplate.Templates.Add(item);
+ }
+ }
+
private void dataGridHazardMaterial_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Row?.Item is HAZPosTemplate template)
diff --git a/ENI2/EditControls/NewDGItemDialog.xaml.cs b/ENI2/EditControls/NewDGItemDialog.xaml.cs
index c1c849e6..c812a78f 100644
--- a/ENI2/EditControls/NewDGItemDialog.xaml.cs
+++ b/ENI2/EditControls/NewDGItemDialog.xaml.cs
@@ -7,7 +7,6 @@ using ENI2.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@@ -85,9 +84,7 @@ namespace ENI2.EditControls
filtered = filtered.Where(elem => elem.TemplateType == sType);
}
this.listBoxDescription.ItemsSource = filtered;
- }
-
+ }
}
-
}
}
diff --git a/ENI2/Excel/ExcelLocalImportHelper.cs b/ENI2/Excel/ExcelLocalImportHelper.cs
index eaee9caa..06ee2abe 100644
--- a/ENI2/Excel/ExcelLocalImportHelper.cs
+++ b/ENI2/Excel/ExcelLocalImportHelper.cs
@@ -398,6 +398,112 @@ namespace ENI2.Excel
#endregion
+ #region Hazard Material Import from Excel
+
+ public static List ImportHazardMaterials()
+ {
+ OpenFileDialog ofd = new OpenFileDialog
+ {
+ 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);
+ var range = worksheet.RangeUsed();
+ var rows = range.RowsUsed().Skip(1); // header row
+
+ List templates = new List();
+
+ foreach (var row in rows)
+ {
+ if (row.Cell(1).IsEmpty() &&
+ row.Cell(3).IsEmpty() &&
+ row.Cell(5).IsEmpty())
+ continue;
+
+ HAZPosTemplate hpt = new HAZPosTemplate();
+
+ if (!row.Cell(1).IsEmpty())
+ hpt.Description = Truncate(row.Cell(1).GetString().Trim(), 100);
+
+ int? hazardIndex = GetIntCell(row.Cell(3));
+ if (hazardIndex.HasValue)
+ hpt.Hazard = (HAZPosTemplate.HazardsEnum)hazardIndex.Value;
+
+ int? flashpointIndex = GetIntCell(row.Cell(5));
+ if (flashpointIndex.HasValue)
+ hpt.Flashpoint = (HAZPosTemplate.FlashpointEnum)flashpointIndex.Value;
+
+ if (!row.Cell(6).IsEmpty())
+ {
+ string specRef = row.Cell(6).GetString().Trim();
+ if (specRef.Equals("Ja", StringComparison.OrdinalIgnoreCase))
+ hpt.SpecRef15_19 = true;
+ else if (specRef.Equals("Nein", StringComparison.OrdinalIgnoreCase))
+ hpt.SpecRef15_19 = false;
+ }
+
+ if (!row.Cell(7).IsEmpty())
+ {
+ string type = row.Cell(7).GetString().Trim();
+ if (Enum.TryParse(type, true, out HAZPosTemplate.SublistType templateType))
+ hpt.TemplateType = templateType;
+ }
+
+ if (!row.Cell(9).IsEmpty())
+ {
+ string mhb = row.Cell(9).GetString().Trim();
+ if (mhb.Equals("y", StringComparison.OrdinalIgnoreCase))
+ hpt.MHB = true;
+ else if (mhb.Equals("n", StringComparison.OrdinalIgnoreCase))
+ hpt.MHB = false;
+ }
+
+ int? imsbcHazIndex = GetIntCell(row.Cell(12));
+ if (imsbcHazIndex.HasValue)
+ hpt.IMSBC_HAZ = (HAZPosTemplate.IMO_HAZ_ClassEnum)imsbcHazIndex.Value;
+
+ if (!row.Cell(13).IsEmpty())
+ hpt.UNNr = Truncate(row.Cell(13).GetString().Trim(), 100);
+
+ if (!row.Cell(14).IsEmpty())
+ hpt.IMOClass = Truncate(row.Cell(14).GetString().Trim(), 100);
+
+ int? pollutionIndex = GetIntCell(row.Cell(16));
+ if (pollutionIndex.HasValue)
+ hpt.PollutionCategory = (HAZPosTemplate.PollutionCategoryEnum)pollutionIndex.Value;
+
+ if (!row.Cell(20).IsEmpty())
+ hpt.FP_IBC = Truncate(row.Cell(20).GetString().Trim(), 100);
+
+ if (!row.Cell(21).IsEmpty())
+ hpt.Comment = Truncate(row.Cell(21).GetString().Trim(), 255);
+
+ hpt.IsDirty = true;
+ templates.Add(hpt);
+ }
+
+ return templates;
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError,
+ MessageBoxButton.OK, MessageBoxImage.Error);
+ }
+ }
+
+ return new List();
+ }
+
+ #endregion
+
private static string Truncate(string value, int maxLength)
{
if (value == null)
@@ -405,5 +511,21 @@ namespace ENI2.Excel
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
+ private static int? GetIntCell(IXLCell cell)
+ {
+ if (cell == null || cell.IsEmpty()) return null;
+ try
+ {
+ return cell.GetValue();
+ }
+ catch
+ {
+ string text = cell.GetString().Trim();
+ if (int.TryParse(text, out int val))
+ return val;
+ }
+ return null;
+ }
+
}
}