Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ


How to export to Excel from vb.net code. by Sorwen
Posted: 26 Sep 07

I see this question a lot and was rather surprised there wasn't a how to.  This is the most effective manner I've found to export to excel without excessive code or getting someone's dll/plug-in/etc.  Some people don't like it because it uses adodb rather than ado.net.


Public Shared Sub DataToExcel(ByVal rTable As ADODB.Recordset, ByVal WorkbookLocation As String, ByVal WorkbookName As String)
        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)

        oBook.SaveAs(WorkbookLocation & WorkbookName)

        oSheet = Nothing
        oBook = Nothing
        oExcel = Nothing
    End Sub

This is the total basics.  There are all kinds of modifications you could do.  In this format you can send it any commands that you would as if you were working in a Excel workbook's VBA.

Auto Format all cells width:



Force save without overwrite confirmation:


            oExcel.DisplayAlerts = False
            oBook.SaveAs(WorkbookLocation & WorkbookName)
            oExcel.DisplayAlerts = True

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close