J
JonOfAllTrades
Good evening, everyone. I have a simple C# program that copies a lot of data
into Excel, with some minimal formatting, and it runs VERY slowly. Once I
have the data (in DataGridView controls), I can write CSV files in seconds,
but the Excel part takes hours. This is the main code, sorry it's not very
readable in this forum:
int r, c;
....
r = 0;
foreach (DataGridViewRow row in grid.Rows)
{
for (c = 0; c < numColumns; /*iterated at bottom of block*/)
{
object value = row.Cells[c].Value;
if (value != null)
{
file.Write(Program.CleanForCSV(value.ToString()));
worksheet.Cells[r+3, c+1] = value; // Skip ahead to row #3, to allow
space for headers
}
if (++c < numColumns) file.Write(','); // We don't want a comma after
the last column
}
if (++r < numRows) file.WriteLine();
}
The odd iteration is the simplest way I could think of to efficiently avoid
trailing commas in CSV.
I've done some simple profiling at the procedure level, and there's no
single part of the export process that takes much longer than the rest. Can
I copy data by row or even copy the content of the whole DataGridView? It
seems like that should be possible, but I haven't seen where. Any ideas?
Thanks!
into Excel, with some minimal formatting, and it runs VERY slowly. Once I
have the data (in DataGridView controls), I can write CSV files in seconds,
but the Excel part takes hours. This is the main code, sorry it's not very
readable in this forum:
int r, c;
....
r = 0;
foreach (DataGridViewRow row in grid.Rows)
{
for (c = 0; c < numColumns; /*iterated at bottom of block*/)
{
object value = row.Cells[c].Value;
if (value != null)
{
file.Write(Program.CleanForCSV(value.ToString()));
worksheet.Cells[r+3, c+1] = value; // Skip ahead to row #3, to allow
space for headers
}
if (++c < numColumns) file.Write(','); // We don't want a comma after
the last column
}
if (++r < numRows) file.WriteLine();
}
The odd iteration is the simplest way I could think of to efficiently avoid
trailing commas in CSV.
I've done some simple profiling at the procedure level, and there's no
single part of the export process that takes much longer than the rest. Can
I copy data by row or even copy the content of the whole DataGridView? It
seems like that should be possible, but I haven't seen where. Any ideas?
Thanks!