Added excel import for initial data set.

This commit is contained in:
Daniel Schick 2026-02-08 08:44:00 +01:00
parent 50ab9c95f5
commit 809366c508
4 changed files with 135 additions and 4 deletions

View File

@ -21,12 +21,14 @@
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="buttonSave" Grid.Column="0" Margin="2" Content="Save all changes" Click="buttonSave_Click" />
<Button x:Name="buttonAdd" Grid.Column="1" Margin="2" Content="Add new" Click="buttonAdd_Click" />
<Button x:Name="buttonImport" Grid.Column="2" Margin="2" Content="{x:Static p:Resources.textImportFromExcel}" Click="buttonImport_Click" />
</Grid>
<local:ENIDataGrid Grid.Row="1" Margin="2,8,2,2" x:Name="dataGridHazardMaterial" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
SelectionMode="Extended" AutoGenerateColumns="False" CellEditEnding="dataGridHazardMaterial_CellEditEnding" CanUserAddRows="False">

View File

@ -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)

View File

@ -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;
}
}
}
}
}

View File

@ -398,6 +398,112 @@ namespace ENI2.Excel
#endregion
#region Hazard Material Import from Excel
public static List<HAZPosTemplate> 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<HAZPosTemplate> templates = new List<HAZPosTemplate>();
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<HAZPosTemplate>();
}
#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<int>();
}
catch
{
string text = cell.GetString().Trim();
if (int.TryParse(text, out int val))
return val;
}
return null;
}
}
}