Results 1 to 5 of 5

Thread: Export Gridview to Excel in VB.Net

  1. #1
    Join Date
    Dec 2008
    Posts
    10

    Export Gridview to Excel in VB.Net

    Hello all,

    I am developing an application using VB.net,
    In that i want to export a contents of gridview to the excel file.
    but i am not able to do proper code for it.
    How can I export datatable or gridview to excel File.

    Thanks.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Export Gridview to Excel in VB.Net

    You can use this code.

    Code:
    Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
           
                Me.lblMessage.Text = ""
                Me.gdvCustomer.AllowPaging = False
                'If you are binding GridView under Code Behind
                'Me.gdvCustomer.DataSource = GetCustomer
     
                Me.gdvCustomer.DataBind()
                Dim tw As New StringWriter()
                Dim hw As New System.Web.UI.HtmlTextWriter(tw)
                Dim frm As HtmlForm = New HtmlForm()
                Page.Response.ContentType = "application/vnd.ms-excel"
               Page.Response.AddHeader("content-disposition", "attachment;Customer.xls")
                Page.Response.Charset = ""
                Page.EnableViewState = False
                frm.Attributes("runat") = "server"
                Controls.Add(frm)
     
                frm.Controls.Add(gdvCustomer)
                frm.RenderControl(hw)
                Response.Write(tw.ToString())
                Response.End()
                Me.gdvCustomer.AllowPaging = True
     
                'If you are binding GridView under Code Behind
                'Me.gdvCustomer.DataBind()
    End Sub

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Export Gridview to Excel in VB.Net

    Check this code.

    Code:
    protected void BtnExport_Click(object sender, EventArgs e)
    {
      Response.Clear();
      Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
      Response.Charset = "";
     
      // If you want the option to open the Excel file without saving then
      // comment out the line below
      // Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.ContentType = "application/vnd.xls";
      System.IO.StringWriter stringWrite = new System.IO.StringWriter();
      System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
      GridView1.RenderControl(htmlWrite);
      Response.Write(stringWrite.ToString());
      Response.End();
    }

  4. #4
    Join Date
    Jun 2010
    Posts
    1

    Re: Export Gridview to Excel in VB.Net

    Hi,

    here is a code snippet how to export DataGrid to Excel in Excel ASP.NET application with GemBox.Spreadsheet Excel .NET library:

    Code:
    // Get DataTable that DataGrid is bound to.
    var dataTable = (DataTable)dataGrid.DataSource;
    
    // Create new ExcelFile.
    var ef = new ExcelFile();
    // Add new worksheet to the file.
    var ws = ef.Worksheets.Add(dataTable.TableName);
    // Insert the data from DataTable to the worksheet starting at cell "A1".
    ws.InsertDataTable(dataTable, "A1", true);
    
    // Stream file to browser.
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
    ef.SaveXls(Response.OutputStream);
    Response.End();

  5. #5
    Join Date
    Jun 2010
    Posts
    4

    Re: Export Gridview to Excel in VB.Net

    Here a code that you Export Grid view to Excel,you tried all are a right,and perfect you do it work,both are a different way but is a very simple to use,try it is working,really a good thing to share with all it help you to all.

Similar Threads

  1. How to export ical to excel
    By Xmen in forum Windows Software
    Replies: 5
    Last Post: 02-09-2010, 07:41 AM
  2. php export to excel
    By Derwin in forum Software Development
    Replies: 3
    Last Post: 25-08-2009, 06:25 PM
  3. Export OLE object to Excel?
    By Kelsey in forum Windows Software
    Replies: 3
    Last Post: 31-03-2009, 09:29 PM
  4. How can i export datatable to excel
    By Leena in forum Software Development
    Replies: 3
    Last Post: 30-01-2009, 11:45 PM
  5. Export to Excel
    By nehal_serpa in forum Microsoft Project
    Replies: 6
    Last Post: 11-12-2008, 12:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,571,872.39186 seconds with 17 queries