Results 1 to 3 of 3

Thread: Reading from Excel using

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    3

    Reading from Excel using

    Hi,

    I have one excel sheet with 6 columns and then data as rows. I want to open that excel sheet and then read data from rows. I want to save that data to access database.

    I am new to vb so i dont know how to read excel sheets using vb.

    Any help will highly appreciated.

    thanks.

  2. #2
    Hyperactive Member vincentg's Avatar
    Join Date
    Jun 2005
    Location
    Chicago IL, USA
    Posts
    261

    Re: Reading from Excel using

    Hi there.

    Option 1: You can use ADO to read the excel file and convert it into a recordset so you can read it again to save it into a database.

    VB Code:
    1. Dim conn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3.  
    4. conn = New ADODB.Connection
    5. conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" _
    6.   & "Data Source=C:\YOUREXCELFILEHERE.XLS;" _
    7.   & "Extended Properties=Excel 8.0;"
    8.  
    9.  
    10. rs.Open("Select * from [YOUREXCELSHEETNAMEHERE]", conn,ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)

    Option 2: You can use a Excel Office Automation to read each cell in the Excel Object then later on save it into a database..Remember to add in your project a reference to Microsoft Excel Object...

    VB Code:
    1. Dim objExcelApp As Excel.Application
    2. Dim objExcelWkb As Excel.Workbook
    3. Dim objExcelWks As Excel.Worksheet
    4.  
    5. Set objExcelApp = New Excel.Application
    6. Set objExcelWkb = objExcelApp.Workbooks.Open(FileName:='YOUREXCELNAME.XLS')
    7. Set objExcelWks = objExcelWkb.Worksheets(1)
    8. objExcelWks.Cells(10,10) '<-- you can use this to access each cell...


    option 3.. option 4 and so on are yours to find out... there are a lot of ways to read it......


    -vince

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Reading from Excel using

    You can do it with one line of code, but from Access instead. Allot easier.
    VB Code:
    1. Application.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Table_XL", "D:\Book1.xls", False, "Sheet2$"
    Moved from Classic VB forum.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width