How to Export Datagridview contents to Excel

csharp , db Nigeria
  • 13 years ago

    I have to export the data present in the datagridview to Microsoft Excel.  It is a window application using c#.  Does anyone know how to do it.  Please help.



  • 13 years ago

    See DataGridView Extension from CompletIT - it's free and you can export to Excel, tml, Pdf and do 1000 more things ;)

  • 13 years ago

    I think this would be useful


            Response.AddHeader("content-disposition", "attachment;filename=filename.xls")
            Response.Charset = ""
            Response.ContentType = "application/vnd.xls"
            Dim stringWrite As New System.IO.StringWriter
            Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
            Dim dGrid As New DataGrid
            dGrid.DataSource = tempDataset


  • 13 years ago

    By using C# is it possible to use the code you have given??

  • 13 years ago

     Hi Nitin,

    Check this link you can get some help from this link. 

  • 13 years ago

    hi nitin004 ,
    You can easily export content of DataGridView to Excel sheet. There are several articles based on it.
    Just checkout the given links.

    Or just checkout the given code. 

    Microsoft.Office.Interop.Excel.Application wapp;
    Worksheet wsheet;
    Workbook wbook;
    wapp =
    new Microsoft.Office.Interop.Excel.Application();
    wapp.Visible =
    wbook = wapp.Workbooks.Add(
    wsheet = (
       int iX;
       int iY;
       for (int i = 0; i < this.DataGridResults.Columns.Count; i++)
           wsheet.Cells[1, i + 1] =
           wsheet.Font.Bold =

       for (int i = 0; i < this.DataGridResults.Rows.Count; i++)
    row = this.DataGridResults.Rows[i];
    for (int j = 0; j < row.Cells.Count; j++)
       DataGridViewCell cell = row.Cells[j];
                       wsheet.Cells[i + 2, j + 1] = (cell.Value ==
    null) ? "" : cell.Value.ToString();
    catch (Exception ex)
        wapp.Visible =
    catch (Exception ex1)
    wapp.UserControl =

    Hope this will help you.
    Thanks & Regards

  • 12 years ago
    I developed this function which i think is fantastic and wants to share it with the rest of the world: //fxn that exports content of a datagridview object to an excel file public static void export_datagridview_to_excel(DataGridView dgv, string excel_file) { int cols; //open file StreamWriter wr = new StreamWriter(excel_file); //determine the number of columns and write columns to file cols = dgv.Columns.Count; for (int i = 0; i < cols; i++) { wr.Write(dgv.Columns[i].Name.ToString().ToUpper() + "\t"); } wr.WriteLine(); //write rows to excel file for (int i = 0; i < (dgv.Rows.Count - 1); i++) { for (int j = 0; j < cols; j++) { if (dgv.Rows[i].Cells[j].Value != null) wr.Write(dgv.Rows[i].Cells[j].Value + "\t"); else { wr.Write("\t"); } } wr.WriteLine(); } //close file wr.Close(); }
  • 12 years ago
    //please import: System.IO
  • 11 years ago


    You can certainly do this with Excel Automation, but I suggest not to use it. For details see Excel Interop issues.

    Try to find some 3rd party library that reads and writes Excel files without use of Excel Automation.

    I use GemBox.Spreadsheet for some time now, and it is great. It is very fast and easy to use.

    Here is an example how to export DataGrid to Excel with GemBox.Spreadsheet library.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook