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.

    Thanks

     

  • 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.Clear()
            Response.AddHeader("content-disposition", "attachment;filename=filename.xls")
            Response.Charset = ""
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            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
            dGrid.DataBind()

            dGrid.RenderControl(htmlWrite)
            Response.Write(stringWrite.ToString())
            Response.End()

  • 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. 

    http://www.eggheadcafe.com/community/aspnet/2/10017590/export-to-excel.aspx 

  • 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.

    http://www.codeproject.com/KB/database/ExportDataToExcelUsingCS.aspx
    http://www.codeproject.com/KB/office/office_automation.aspx
    http://www.codeproject.com/KB/cs/export2excel.aspx
    http://www.codeproject.com/KB/dotnet/ExportToExcel.aspx?df=100&forumid=146533&exp=0&select=1357703
    http://www.dotnetspider.com/qa/Question45425.aspx

    Or just checkout the given code. 

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

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

    Hope this will help you.
    Thanks & Regards
    Dilipv

  • 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

    Hi!

    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).

Contribute

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