first steps excel import berths

This commit is contained in:
Daniel Schick 2023-08-18 16:09:13 +02:00
parent 411ea8135e
commit ba170c9320
4 changed files with 64 additions and 1 deletions

View File

@ -0,0 +1,4 @@
[*.cs]
# Default severity for all analyzer diagnostics
dotnet_analyzer_diagnostic.severity = none

View File

@ -14,6 +14,9 @@ using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using brecal.model;
using brecal.mysql;
using ExcelDataReader;
using Microsoft.Win32;
namespace RoleEditor
{
@ -572,7 +575,57 @@ namespace RoleEditor
private void buttonImportBerths_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "Excel Files|*.xls;*.xlsx"
};
if (ofd.ShowDialog() ?? false)
{
FileStream stream;
try
{
stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
List<Berth> importBerthList = new();
try
{
do
{
while (reader.Read())
{
if (reader.FieldCount < 2)
{
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);
}
} while (reader.NextResult());
}
catch (Exception ex)
{
MessageBox.Show("Error reading Excel: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
stream.Close();
}
}
private void buttonImportShipss_Click(object sender, RoutedEventArgs e)

View File

@ -28,6 +28,7 @@
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="ExcelDataReader" Version="3.7.0-develop00385" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.5.0" />
</ItemGroup>

View File

@ -7,7 +7,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoleEditor", "RoleEditor.cs
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "brecal.model", "..\brecal.model\brecal.model.csproj", "{F3BC5ADC-BF57-47DC-A5D5-CC4A13857DEE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "brecal.mysql", "..\brecal.mysql\brecal.mysql.csproj", "{E88F908B-48C9-46BD-A3AE-C36FBE9EDF1F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "brecal.mysql", "..\brecal.mysql\brecal.mysql.csproj", "{E88F908B-48C9-46BD-A3AE-C36FBE9EDF1F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{17164AE2-AB25-4394-A865-4A8CF524D6DA}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution