Gridview to Excel

Hi friends,

This is the code snippet for creating excel file with the details of grid view in windows application:
 
  1. public void generateExcel()   
  2.  {   
  3.      string GivenFileName = null;   
  4.      if (gvData.Rows.Count > 0)   
  5.      {   
  6.          DialogResult Result = saveFileDialog1.ShowDialog();//Show the dialog to save the file.   
  7.    
  8.          if ((Result == DialogResult.OK) && (saveFileDialog1.FileName.Length > 0))   
  9.          {   
  10.              GivenFileName = saveFileDialog1.FileName;   
  11.              Microsoft.Office.Interop.Excel.Application ExcelApp = new 
                 Microsoft.Office.Interop.Excel.Application();   
  12.              ExcelApp.Application.Workbooks.Add(Type.Missing);   
  13.              ExcelApp.Columns.ColumnWidth = 20;   
  14.                
  15.              // Storing header part in Excel   
  16.    
  17.              for (int i = 1; i < gvData.Columns.Count + 1; i++)   
  18.              {   
  19.                  ExcelApp.Cells[1, i] = gvData.Columns[i - 1].HeaderText;   
  20.              }   
  21.    
  22.              for (int i = 0; i < gvData.Rows.Count; i++)   
  23.              {   
  24.                  for (int j = 0; j < gvData.Columns.Count; j++)   
  25.                  {   
  26.                      ExcelApp.Cells[i + 2, j + 1] = gvData.Rows[i].Cells[j].Value.ToString();   
  27.                  }   
  28.              }   
  29.    
  30.              string location = GivenFileName;   
  31.              ExcelApp.ActiveWorkbook.SaveCopyAs(location + ".xlsx");   
  32.              ExcelApp.ActiveWorkbook.Saved = true;   
  33.              ExcelApp.Quit();   
  34.              MessageBox.Show("Details saved in excel !.");   
  35.          }   
  36.      }   
  37.      else   
  38.          MessageBox.Show("Nothing to upload !.");   
  39.  }