Select Page
Codoid Blog

Getting Excel Column Names using ADODB

Being a leading QA Company, we write blogs on all prominent software testing topics and tools using our real-world experience. So stay sharp by subscribing to our Newsletter.

Getting Excel columns names using ADODB is simpler than Excel COM interface. The below code shows how to retrieve column names from XLS.

Code

strExcelPath="C:Book1.xls"
strTableName="Sheet1$"'Note: $ symbol should be included with Table Name.
Set oCon=CreateObject("ADODB.Connection")

With oCon
.Provider = "MSDASQL"
.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & _
"DBQ="&strExcelPath&"; ReadOnly=False;"
.Open
End With

Set oRs1=oCon.OpenSchema(4,Array(Empty, Empty,strTableName,Empty))

Do Until oRS1.EOF
Msgbox oRs1.Fields.Item("COLUMN_NAME").Value
oRS1.MoveNext
Loop

oRs1.Close
oCon.Close

Set oRs1=Nothing
Set oCon=Nothing
  
Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility