2
I have a DataGridView
where in it there is an average of about 50 thousand lines where these should be exported to an Excel file.
But because of the huge amount of data the application simply freezes and does not end, I am using the library Microsoft.Office.Interop.Excel the button code is this:
private void btnExportar_Click(object sender, EventArgs e)
{
//exportando arquivos para o excel
if (dgvDados.Rows.Count > 0)
{
try
{
XcelApp.Application.Workbooks.Add(Type.Missing);
for (int i = 1; i < dgvDados.Columns.Count + 1; i++)
{
XcelApp.Cells[1, i] = dgvDados.Columns[i - 1].HeaderText;
}
//
for (int i = 0; i < dgvDados.Rows.Count - 1; i++)
{
for (int j = 0; j < dgvDados.Columns.Count; j++)
{
XcelApp.Cells[i + 2, j + 1] = dgvDados.Rows[i].Cells[j].Value.ToString();
}
}
//
XcelApp.Columns.AutoFit();
//
XcelApp.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show("Erro : " + ex.Message);
XcelApp.Quit();
}
}
}
Is there any way to export something from this level to excel?
It tries to create Paggings, ex: if there are 50 thousand lines it makes him go to the bank and search every 10 thousand, opening several requests within each pagination.
– Pedro
The application is freezing because it is processing a lot in the main thread, if you create a new thread to run it the application will not freeze
– Lucas Riechelmann Ramos
Explain better what you mean by "lock", some error occurs, the process does not end or only takes time and the user interface freezes until the finish?
– Leandro Angelo
@Leandroangelo the interface freezes, but I’ve never seen it finish and I’ve left a matter of 1 h and still remains frozen
– Patrick Perdigão
debugging, you measured how long it is taking per line, although the large volume does not have as much complexity as...
– Leandro Angelo
I did the test now by line ended up being very variable, for example when I open the application and export for the first time it takes about 5s but after running the first time it takes around 0.5s. I tested with 7mil lines and took 15 min
– Patrick Perdigão
the same problem is the application stay frozen without being able to do anything, I believe thread is really a good option but do not know mt well the feature I will give a search
– Patrick Perdigão
but still if follow by this reasoning 7 thousand lines = 15 min 50 thousand lines =~ 2h is long time
– Patrick Perdigão