Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. exporting datagrid to excel in vb.net

exporting datagrid to excel in vb.net

Scheduled Pinned Locked Moved Visual Basic
csharphelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joshunleashed
    wrote on last edited by
    #1

    I want to display data in a datagrid and then want to export that data to excel in vb.net.plz help me ASAP. thnx josh

    A 1 Reply Last reply
    0
    • J joshunleashed

      I want to display data in a datagrid and then want to export that data to excel in vb.net.plz help me ASAP. thnx josh

      A Offline
      A Offline
      albCode
      wrote on last edited by
      #2

      <%@ Import Namespace="System.Data.OleDb" %> <%@ Import Namespace="System.Data" %> Dim MyConnection As OleDbConnection Public Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("database/utenti.mdb")) BindGrid("id") End Sub Sub BindGrid(ByVal SortField As String) Dim DS As DataSet Dim MyCommand As OleDbDataAdapter MyCommand = New OleDbDataAdapter("select * from utenti", MyConnection) DS = New DataSet MyCommand.Fill(DS, "utenti") Dim Source As DataView = DS.Tables("utenti").DefaultView Source.Sort = SortField MyDataGrid.DataSource = Source MyDataGrid.DataBind() End Sub Sub ToExcel(sender As Object, e As System.EventArgs) Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) MyDataGrid.RenderControl(hw) Response.Write(tw.ToString()) Response.End() End Sub _____________________ Proud to be Albanian _____________________

      G 1 Reply Last reply
      0
      • A albCode

        <%@ Import Namespace="System.Data.OleDb" %> <%@ Import Namespace="System.Data" %> Dim MyConnection As OleDbConnection Public Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("database/utenti.mdb")) BindGrid("id") End Sub Sub BindGrid(ByVal SortField As String) Dim DS As DataSet Dim MyCommand As OleDbDataAdapter MyCommand = New OleDbDataAdapter("select * from utenti", MyConnection) DS = New DataSet MyCommand.Fill(DS, "utenti") Dim Source As DataView = DS.Tables("utenti").DefaultView Source.Sort = SortField MyDataGrid.DataSource = Source MyDataGrid.DataBind() End Sub Sub ToExcel(sender As Object, e As System.EventArgs) Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) MyDataGrid.RenderControl(hw) Response.Write(tw.ToString()) Response.End() End Sub _____________________ Proud to be Albanian _____________________

        G Offline
        G Offline
        Greeky
        wrote on last edited by
        #3

        if i work on Vb.net (not asp.net) , what will i do?

        D 1 Reply Last reply
        0
        • G Greeky

          if i work on Vb.net (not asp.net) , what will i do?

          D Offline
          D Offline
          Dean_SF
          wrote on last edited by
          #4

          I subclassed a datagrid and use this code: Dim x As Integer, y As Integer Dim wb As Excel.Workbook Dim ws As Excel.Worksheet Dim oExcel As Excel.Application Static SheetCount As Integer = 1 Dim tbl As DataTable = CType(Me.DataSource, DataTable) Cursor.Current = Cursors.WaitCursor If Not Me.ReadOnly Then Dim changedRows As New ArrayList Dim row As DataRow For Each row In tbl.Rows If row.RowState <> DataRowState.Unchanged Then changedRows.Add(row) End If Next If changedRows.Count > 0 Then Cursor.Current = Cursors.Default MsgBox("There are pending data updates for this grid. Please save these updates to the database prior to exporting the data to Excel.", MsgBoxStyle.Information, "Pending Updates") Exit Sub End If End If If oExcel Is Nothing Then oExcel = New Excel.Application End If wb = oExcel.Workbooks.Add ws = wb.Sheets.Add ws.Name = "Export" & SheetCount SheetCount = SheetCount + 1 'write column headers For x = 0 To tbl.Columns.Count - 1 ws.Range(Chr(65 + x) & "1").Value = tbl.Columns(x).ColumnName ws.Range(Chr(65 + x) & "1").Font.Bold = True Next 'write data For x = 0 To tbl.Rows.Count - 1 For y = 0 To tbl.Columns.Count - 1 ws.Range(Chr(65 + y) & (x + 2).ToString).Value = tbl.Rows(x).Item(y) Next Next oExcel.Application.Visible = True ' Release Application object and allow Excel to be closed by user: If Not oExcel.UserControl Then oExcel.UserControl = True System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) oExcel = Nothing Cursor.Current = Cursors.Default As you can see there are several references to "Me" in this routine for my use. In your case you would simply replace "Me" with the name of your datagrid. You will also need to add a reference to Excel in your project. Hope this works for you. Dean

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups