Export Gird View to Excel in C#: Is Very Slow for Large Amount Data

  • Thread starter Thread starter farzad khajeh
  • Start date Start date
F

farzad khajeh

Guest
Hi Dears

I Have a Class Code for Exporting Grid View to Excel File , it work find but for Large amount data , it is very slow

is any one help me ??

My Code is :

class GridviewToExcel
{
public static void Form(DataGridView grv)
{
Excel.Application app = new Excel.Application();
//app.Visible = true;
Excel.Workbook wb = app.Workbooks.Add(1);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
// changing the name of active sheet
ws.Name = "Exported";

ws.Rows.HorizontalAlignment = HorizontalAlignment.Center;
// storing header part in Excel
for (int i = 1; i < grv.Columns.Count + 1; i++)
{
ws.Cells[1, i] = grv.Columns[i - 1].HeaderText;
}

// storing Each row and column value to excel sheet
for (int i = 0; i < grv.Rows.Count; i++)
{
for (int j = 0; j < grv.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = grv.Rows.Cells[j].Value.ToString();
}
}
// sizing the columns
ws.Cells.EntireColumn.AutoFit();
app.Visible = true;
}
}


thanks before

farzad khajeh

Continue reading...
 
Back
Top