Add Distinta Base import tab: parse .xls/.xml export to CSV with preview

Reads the ERP-exported "Distinta Base" file (SpreadsheetML XML or real
binary .xls/.xlsx via ExcelDataReader), extracts Componente/Descrizione,
writes a CSV and previews it in a new tab alongside the existing CAD
search. Also ignore the exported Excel/CSV files dropped in the repo root.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5 committed 2026-09-16 10:22:16 +02:00
1 parent 82a69e0d20
commit f88639bf4b
4 files changed
+209 -3

No files matched your search

+92 -3
View File
@@ -15,6 +15,8 @@ public partial class Form1 : Form
private readonly DataGridView _grid = new();
private readonly Label _lblStatus = new();
private readonly Label _lblDb = new();
private readonly DataGridView _gridImport = new();
private readonly Label _lblImportStatus = new();
private const string OpzionePrtAsm = "Solo prt/asm (predefinito)";
private const string OpzioneTuttiTipi = "Tutti i tipi";
@@ -120,9 +122,96 @@ public partial class Form1 : Form
_lblStatus.Text = "Pronto.";
statusPanel.Controls.Add(_lblStatus);
Controls.Add(_grid);
Controls.Add(statusPanel);
Controls.Add(topPanel);
var tabRicerca = new TabPage("Ricerca CAD");
tabRicerca.Controls.Add(_grid);
tabRicerca.Controls.Add(statusPanel);
tabRicerca.Controls.Add(topPanel);
var tabs = new TabControl { Dock = DockStyle.Fill };
tabs.TabPages.Add(BuildTabImportaDistinta());
tabs.TabPages.Add(tabRicerca);
Controls.Add(tabs);
}
private TabPage BuildTabImportaDistinta()
{
var tab = new TabPage("Importa Distinta Base") { AllowDrop = true };
var lblIstruzioni = new Label
{
Dock = DockStyle.Top,
Height = 60,
Padding = new Padding(8),
Text = "Trascina qui il file .xls/.xml esportato dal gestionale (foglio 'Distinta "
+ "Base'), oppure scegli il file col pulsante sotto. Verrà generato un CSV con "
+ "le colonne Componente e Descrizione, con anteprima qui sotto.",
};
var btnSfoglia = new Button { Text = "Scegli file...", Dock = DockStyle.Top, Height = 32 };
btnSfoglia.Click += (_, _) => ScegliFileDistinta();
_lblImportStatus.Dock = DockStyle.Top;
_lblImportStatus.Height = 24;
_lblImportStatus.Padding = new Padding(8, 4, 8, 4);
_lblImportStatus.ForeColor = SystemColors.GrayText;
_gridImport.Dock = DockStyle.Fill;
_gridImport.ReadOnly = true;
_gridImport.AllowUserToAddRows = false;
_gridImport.AllowUserToDeleteRows = false;
_gridImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
_gridImport.Columns.Add("componente", "Componente");
_gridImport.Columns.Add("descrizione", "Descrizione");
tab.Controls.Add(_gridImport);
tab.Controls.Add(_lblImportStatus);
tab.Controls.Add(btnSfoglia);
tab.Controls.Add(lblIstruzioni);
tab.DragEnter += (_, e) =>
e.Effect = e.Data?.GetDataPresent(DataFormats.FileDrop) == true
? DragDropEffects.Copy
: DragDropEffects.None;
tab.DragDrop += (_, e) =>
{
if (e.Data?.GetData(DataFormats.FileDrop) is string[] { Length: > 0 } file)
ImportaDistinta(file[0]);
};
return tab;
}
private void ScegliFileDistinta()
{
using var dialog = new OpenFileDialog
{
Filter = "File distinta base (*.xls;*.xml)|*.xls;*.xml|Tutti i file|*.*",
Title = "Seleziona il file Distinta Base",
};
if (dialog.ShowDialog(this) == DialogResult.OK)
ImportaDistinta(dialog.FileName);
}
private void ImportaDistinta(string percorso)
{
try
{
var (csv, righe) = DistintaBaseImporter.ConvertiInCsv(percorso);
_gridImport.Rows.Clear();
foreach (var (componente, descrizione) in righe)
{
_gridImport.Rows.Add(componente, descrizione);
}
_lblImportStatus.ForeColor = Color.DarkGreen;
_lblImportStatus.Text = $"CSV generato ({righe.Count} righe): {csv}";
}
catch (Exception ex)
{
_gridImport.Rows.Clear();
_lblImportStatus.ForeColor = Color.DarkRed;
_lblImportStatus.Text = $"Errore durante la conversione: {ex.Message}";
}
}
private void InizializzaDatabase()