Export GridView Data to EXCEL in ASP.Net

EXPORT GRIDVIEW DATA TO EXCEL FILE

C# .Net
  • First add the below namespace in your application.

    using Excel = Microsoft.Office.Interop.Excel; 

     

  •  Write below code your code behind file on Button Click Event

    Excel.Application xlApp;

    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;


    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Add(misValue);

    xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);



     int i = 0;
     int j = 0;
      


                 for (i = 0; i
                   {
                      for (j = 0; j
                       {
                             DataGridViewCell cell = dgfess[j, i];
                             xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                        }
                    }xlWorkBook.SaveAs(“Students.xls”, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);


    xlApp.Quit();

     MessageBox.Show(“Excel file created , you can find the file ‘Students.xls’ in My Document”, ” Meera Academy”);


Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 9 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">