Excel Import berth in RoleEditor Tool

This commit is contained in:
Daniel Schick 2023-08-21 08:26:32 +02:00
parent 8939339d09
commit beb9d75a90
2 changed files with 56 additions and 13 deletions

View File

@ -34,9 +34,9 @@
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="160" /> <ColumnDefinition Width=".4*" />
<ColumnDefinition Width=".38*" /> <ColumnDefinition Width=".2*" />
<ColumnDefinition Width=".62*" /> <ColumnDefinition Width=".4*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<ListBox x:Name="listBoxParticipant" Margin="2" Grid.RowSpan="7" SelectionChanged="listBoxParticipant_SelectionChanged"> <ListBox x:Name="listBoxParticipant" Margin="2" Grid.RowSpan="7" SelectionChanged="listBoxParticipant_SelectionChanged">
<ListBox.ContextMenu> <ListBox.ContextMenu>

View File

@ -45,6 +45,7 @@ namespace RoleEditor
{ {
InitializeComponent(); InitializeComponent();
_dbManager = new(); _dbManager = new();
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // for ExcelDataReader on .NET Core
} }
private async void Window_Loaded(object sender, RoutedEventArgs e) private async void Window_Loaded(object sender, RoutedEventArgs e)
@ -573,7 +574,7 @@ namespace RoleEditor
#region Excel import #region Excel import
private void buttonImportBerths_Click(object sender, RoutedEventArgs e) private async void buttonImportBerths_Click(object sender, RoutedEventArgs e)
{ {
OpenFileDialog ofd = new OpenFileDialog OpenFileDialog ofd = new OpenFileDialog
{ {
@ -595,6 +596,8 @@ namespace RoleEditor
using (var reader = ExcelReaderFactory.CreateReader(stream)) using (var reader = ExcelReaderFactory.CreateReader(stream))
{ {
List<Berth> importBerthList = new(); List<Berth> importBerthList = new();
int bCounter = 0;
int pCounter = 0;
try try
{ {
@ -606,25 +609,65 @@ namespace RoleEditor
{ {
throw new InvalidDataException("Sheet must have at least 2 Columns of data"); throw new InvalidDataException("Sheet must have at least 2 Columns of data");
} }
Berth b = new Berth();
if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue;
if (!reader.IsDBNull(0)) b.Name = reader.GetString(0);
string participant_name;
if (!reader.IsDBNull(1)) participant_name = reader.GetString(1);
importBerthList.Add(b); if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue;
string berth_name = "";
if (!reader.IsDBNull(0)) berth_name = reader.GetString(0);
if (berth_name.Equals("Liegeplatz", StringComparison.OrdinalIgnoreCase)) continue;
string participant_name = "";
if (!reader.IsDBNull(1)) participant_name = reader.GetString(1);
// find berth in existing list
if (_berths.Any(predicate: x => (x.Name != null) && x.Name.Equals(berth_name, StringComparison.OrdinalIgnoreCase)))
continue;
Berth b = new Berth();
b.Name = berth_name;
bool found_participant = false;
foreach(Participant p in this._participants)
{
if ((p.Name != null) && p.Name.Contains(participant_name, StringComparison.OrdinalIgnoreCase))
{
b.Participant_Id = p.Id;
found_participant = true;
break;
}
}
if (!found_participant)
{
// create new participant
Participant p = new Participant();
p.Name = participant_name;
await p.Save(_dbManager);
_participants.Add(p);
pCounter++;
b.Participant_Id = p.Id;
}
await b.Save(_dbManager);
_berths.Add(b);
bCounter++;
} }
} while (reader.NextResult()); } while (reader.NextResult());
if((pCounter > 0) || (bCounter > 0))
{
MessageBox.Show($"Imported {bCounter} berths and added {pCounter} participants while doing so");
}
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show("Error reading Excel: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show("Error reading Excel: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
} }
} }
stream.Close(); stream.Close();
} }
} }