|
-
Oct 3rd, 2005, 10:06 AM
#1
Thread Starter
New Member
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.
-
Oct 3rd, 2005, 10:35 AM
#2
Hyperactive Member
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:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
conn = New ADODB.Connection
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\YOUREXCELFILEHERE.XLS;" _
& "Extended Properties=Excel 8.0;"
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:
Dim objExcelApp As Excel.Application
Dim objExcelWkb As Excel.Workbook
Dim objExcelWks As Excel.Worksheet
Set objExcelApp = New Excel.Application
Set objExcelWkb = objExcelApp.Workbooks.Open(FileName:='YOUREXCELNAME.XLS')
Set objExcelWks = objExcelWkb.Worksheets(1)
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
-
Oct 3rd, 2005, 11:25 AM
#3
Re: Reading from Excel using
You can do it with one line of code, but from Access instead. Allot easier.
VB Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|