I need to load two spreadsheets to generate a dictionary with spreadsheet data.
I'm a bit confused about this .. I've created a ReadFilter method for not repeating the code, since the process is the same for both files. This method returns a dictionary with the data.
public object LerPlanilha(string range, FileInfo planilha)
{
using (var pck = new ExcelPackage(planilha))
{
var worksheet = pck.Workbook.Worksheets[1];
var cells = worksheet.Cells;
var dictionary = cells[range]
.GroupBy(c => new { c.Start.Row, c.Start.Column })
.ToDictionary(
rcg => new KeyValuePair<int, int>(rcg.Key.Row, rcg.Key.Column),
rcg => cells[rcg.Key.Row, rcg.Key.Column].Value);
return dictionary;
}
}
I need to double-click on two different buttons to load the data from one worksheet at a time, in the btnModel I assign the return of LerFile in the global variable LoadedCells that is an object. I'm doing it this way
private void btnModelo_Click(object sender, EventArgs e)
{
var arquivo = CarregarArquivo().ToString();
if (arquivo != "OK")
{
var planilhaBase = new FileInfo(arquivo);
CelulasCarregadasModelo = LerPlanilha("B16:AG16", planilhaBase);
lblModeloCarregado.Visible = true;
lblModeloNome.Visible = true;
lblModeloCarregado.Text = "Planilha Carregada";
lblModeloNome.Text = Path.GetFileNameWithoutExtension(arquivo);
}
else
{
lblModeloCarregado.Visible = true;
lblModeloCarregado.Text = "Erro/Modelo não selecionado";
}
}
And in the other button btnPlan assigns the return of LerArquivo in CellsLavagedPlan
private void btnPlan_Click(object sender, EventArgs e)
{
var arquivo = CarregarArquivo().ToString();
if (arquivo != "OK")
{
var planilhaBase = new FileInfo(arquivo);
CelulasCarregadasPlan = LerPlanilha("B16:AG16", planilhaBase);
lblFluxoIntCarregado.Visible = true;
lblFluxoIntNome.Visible = true;
lblFluxoIntCarregado.Text = "Planilha Carregada";
lblFluxoIntNome.Text = Path.GetFileNameWithoutExtension(arquivo);
}
else
{
lblFluxoIntCarregado.Visible = true;
lblFluxoIntCarregado.Text = "Erro/Modelo não selecionado";
}
}
Everything happens without problems, the variable is populated with the dictionary.
What happens is that the variables are cleaned after the end of the button method, I can not leave them loaded and then compare the two dictionaries in another method.