Excel Import berth in RoleEditor Tool
This commit is contained in:
parent
8939339d09
commit
beb9d75a90
@ -34,9 +34,9 @@
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="160" />
|
||||
<ColumnDefinition Width=".38*" />
|
||||
<ColumnDefinition Width=".62*" />
|
||||
<ColumnDefinition Width=".4*" />
|
||||
<ColumnDefinition Width=".2*" />
|
||||
<ColumnDefinition Width=".4*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<ListBox x:Name="listBoxParticipant" Margin="2" Grid.RowSpan="7" SelectionChanged="listBoxParticipant_SelectionChanged">
|
||||
<ListBox.ContextMenu>
|
||||
|
||||
@ -45,6 +45,7 @@ namespace RoleEditor
|
||||
{
|
||||
InitializeComponent();
|
||||
_dbManager = new();
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // for ExcelDataReader on .NET Core
|
||||
}
|
||||
|
||||
private async void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
@ -573,7 +574,7 @@ namespace RoleEditor
|
||||
|
||||
#region Excel import
|
||||
|
||||
private void buttonImportBerths_Click(object sender, RoutedEventArgs e)
|
||||
private async void buttonImportBerths_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
OpenFileDialog ofd = new OpenFileDialog
|
||||
{
|
||||
@ -595,6 +596,8 @@ namespace RoleEditor
|
||||
using (var reader = ExcelReaderFactory.CreateReader(stream))
|
||||
{
|
||||
List<Berth> importBerthList = new();
|
||||
int bCounter = 0;
|
||||
int pCounter = 0;
|
||||
|
||||
try
|
||||
{
|
||||
@ -606,25 +609,65 @@ namespace RoleEditor
|
||||
{
|
||||
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());
|
||||
|
||||
if((pCounter > 0) || (bCounter > 0))
|
||||
{
|
||||
MessageBox.Show($"Imported {bCounter} berths and added {pCounter} participants while doing so");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Error reading Excel: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
stream.Close();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user