You are here:   Home
Register   |  Login

Captured Technology - Blog

Minimize

Export and ADO Recordset to Excel

Jan 29

Written by:
1/29/2009 11:55 AM  RssIcon

Export and ADO Recordset to Excel

If you're in need to taking an ADO Recordset from an Access Database of a .Net Application and saving that data programmatically to an Excel file, the following procedure will do a very good job of completing the conversion.

By Steve Patterson

You can modify the SaveAs command towards the end of the function to save the file in a different location with a different name or you can add the value as a parameter to the subroutine. There is also a Visible flag near the end of the procedure which will show the Excel file after it is created. This would also be a very good value to pass into the subroutine.

Private Sub ExportToExcel(ByVal rs As ADODB.Recordset)
 
    Dim oApp As New Excel.Application
    Dim oBook As Excel.Workbook
    Dim oWorkSheet As Excel.Worksheet
    Dim oField As ADODB.Field
    Dim c As Long
    Dim i As Long
    
    On Error GoTo ErrorHandler
 
    Set oBook = oApp.Workbooks.Add
    Set oWorkSheet = oBook.Worksheets.Item(1)
    
    oApp.Visible = False
    
    With oWorkSheet
        c = Asc("A")
        For Each oField In rs.Fields
            .Range(Chr(c) & "1").Value = oField.Name
            .Range(Chr(c) & "1").Font.Bold = True
            c = c + 1
        Next
        
        i = 2
        If rs.RecordCount > 0 Then rs.MoveFirst
        While Not rs.EOF
            c = Asc("A")
            For Each oField In rs.Fields
                .Range(Chr(c) & i).Value = rs(oField.Name)
                c = c + 1
            Next
            i = i + 1
            rs.MoveNext
        Wend
    End With
    
    oWorkSheet.SaveAs "c:\temp\temp.xls"
    'oApp.Visible = True
    oApp.Quit
    
    GoTo CleanExit
 
ErrorHandler:
    MsgBox Err.Number & ": " & Err.Description
 
CleanExit:    
    If Not oApp Is Nothing Then Set oApp = Nothing
    If Not oBook Is Nothing Then Set oBook = Nothing
    If Not oWorkSheet Is Nothing Then Set oWorkSheet = Nothing
 
End Sub

Tags:
Categories:

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

FaceBook

Minimize

Mobile Version

Minimize
Add CapturedTech - Technology Mippin widget

Translate

Minimize

Sponsors

Minimize
Fast and Free Expedited Shipping on orders over $59 offer applies to Bookbyte inventory only - 160x600 banner

Recent Comments

Minimize
Re: Three Benefits to Using a Royalty-Free Image on Your Website or Blog
Don't pay for royalty free images. They are free after all! I offer links to 96 websites that have tens of thousands (maybe even hundreds of thousands) of absolutely free images, including ones for commercial use! All you need to do is subscribe to my blog at www.davesinternetmarketing.info/royalty-free-images-free-for-commercial-use/
Re: Buzzom Offers Featured User Options
This is when discovered this great new way to grow your follow list. You can pay between $10 and $100 for the number of times you want to app ar at the top of the list.
Re: Microsoft Test Bounties
I had spent a good deal of my time looking for someone to explain this subject clearly and I have really enjoyed reading your blog posts and I collected a lot of interesting things as well as I done a research on the subject and learn most peoples will agree with your blog.
Re: 10 Ways to Source Great Sales Leads
Hoovers.com, that have contact information for tens of millions of businesses. If necessary, you can work with these companies' research teams to order lists specifically tailored to your organization's sales needs.
Re: Microsoft Test Bounties
To find bugs and showing it to microsoft is quite admirable. Keep blogging more i will appreciate.

u comment, i follow