Replaced Waste manual excel import with ClosedXML implementation

This commit is contained in:
Daniel Schick 2025-10-04 09:31:40 +02:00
parent dbf15539d9
commit a8adcb3167

View File

@ -3,9 +3,10 @@
//
using bsmd.database;
using ClosedXML.Excel;
using ENI2.EditControls;
using ExcelDataReader;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -517,43 +518,31 @@ namespace ENI2.SheetDisplayControls
ofd.Filter = "Excel Files|*.xls;*.xlsx";
if (ofd.ShowDialog() ?? false)
{
FileStream stream;
try
{
stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
}
catch (Exception ex)
using (var workbook = new XLWorkbook(ofd.FileName))
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var worksheet = workbook.Worksheet(1); // Get first worksheet
var rows = worksheet.RangeUsed().RowsUsed().Skip(3); // Skip first three rows
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
List<Waste> importWasteList = new List<Waste>();
try
{
do
{
// skip first three rows
reader.Read();
reader.Read();
reader.Read();
int cnt = 0;
object o = null;
// Diese Funktion kann das "alte" Sheet Format nicht mehr einlesen!
while (reader.Read() && (cnt < 35))
foreach (var row in rows)
{
if (reader.FieldCount < 9)
if (cnt >= 35) break; // Maximum 35 rows
if (worksheet.RangeUsed().ColumnCount() < 9)
{
throw new InvalidDataException("Sheet must have 9 Columns of data");
}
if (!reader.IsDBNull(1)) o = reader.GetValue(1); else o = null;
object o = null;
if (!row.Cell(2).IsEmpty()) o = row.Cell(2).Value; else o = null;
if ((o != null) && Int32.TryParse(o.ToString(), out int wasteType))
{
Waste waste = _was.GetWasteForType(wasteType);
@ -571,22 +560,22 @@ namespace ENI2.SheetDisplayControls
waste.IsDirty = true;
}
if (!reader.IsDBNull(4)) waste.WasteDescription = reader.GetString(4);
if (!row.Cell(5).IsEmpty()) waste.WasteDescription = row.Cell(5).GetString();
if (waste.WasteDescription.IsNullOrEmpty())
waste.WasteDescription = "-";
if (!reader.IsDBNull(5)) o = reader.GetValue(5); else o = null;
if (!row.Cell(6).IsEmpty()) o = row.Cell(6).Value; else o = null;
if (o != null) waste.WasteDisposalAmount_MTQ = Convert.ToDouble(o);
if (!reader.IsDBNull(6)) o = reader.GetValue(6); else o = null;
if (!row.Cell(7).IsEmpty()) o = row.Cell(7).Value; else o = null;
if (o != null) waste.WasteCapacity_MTQ = Convert.ToDouble(o);
if (!reader.IsDBNull(7)) o = reader.GetValue(7); else o = null;
if (!row.Cell(8).IsEmpty()) o = row.Cell(8).Value; else o = null;
if (o != null) waste.WasteAmountRetained_MTQ = Convert.ToDouble(o);
if (!reader.IsDBNull(8)) waste.WasteDisposalPort = reader.GetString(8).ToUpper();
if (!row.Cell(9).IsEmpty()) waste.WasteDisposalPort = row.Cell(9).GetString().ToUpper();
if (!reader.IsDBNull(9)) o = reader.GetValue(9); else o = null;
if (!row.Cell(10).IsEmpty()) o = row.Cell(10).Value; else o = null;
if (o != null) waste.WasteAmountGeneratedTillNextPort_MTQ = Convert.ToDouble(o);
importWasteList.Add(waste);
@ -594,13 +583,6 @@ namespace ENI2.SheetDisplayControls
}
}
} while (reader.NextResult());
}
catch (Exception ex)
{
MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error);
}
if (importWasteList.Count > 0)
{
this.dataGridWaste.Items.Refresh();
@ -608,9 +590,11 @@ namespace ENI2.SheetDisplayControls
MessageBox.Show(String.Format(Properties.Resources.textWasteImported, importWasteList.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information);
}
}
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}