Reading Excel

download Reading Excel

of 3

Transcript of Reading Excel

  • 8/6/2019 Reading Excel

    1/3

    '===============================================================' Name: Class clsTestData'' Purpose: Creates a TestData Dictionary'' Functions:' BuildContext (Private)' Load (Public)'' Properties:' oDict'' Author: Anshoo Arora'' Version: 0.1'===============================================================Class clsTestData

    Private mDict 'Local Instance of Scripting.DictionaryPublic sWorkBook 'Excel WorkBookPublic vSheet 'Excel WorkSheet

    Public iRow 'Excel Row where test data is contained

    '=======================================================' Name: Function Load'' Purpose: This function makes the TestData dictionary available' to the test.'' Input:' sWorkBook: Path to the Workbook where test data is stored' vSheet : Name of the Worksheet where the data is stored' iRow : Row where the data is retrieved from'

    ' Output:' Object- Scripting.Dictionary'=======================================================Public Default Function Load(sWorkBook, vSheet, iRow)

    With Me.sWorkBook = sWorkBook.vSheet = vSheet.iRow = iRow

    End With

    BuildContext : Set Load = oDictEnd Function

    '=======================================================' Name: Function BuildContext'' Purpose: This function does the core operation of building the' test data dictionary.'' Input:' None'' Output:

  • 8/6/2019 Reading Excel

    2/3

    ' None'=======================================================Private Function BuildContext

    Dim oConn, oRS, arrData, xCONST adOpenStatic = 3CONST adLockOptimistic = 3CONST adCmdText = "&H0001"

    Set oConn = CreateObject("ADODB.Connection")Set oRS = CreateObject("ADODB.RecordSet")

    'Open ConnectionoConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" &_

    "Data Source=" & Me.sWorkBook & ";" & _"Extended Properties=""Excel 8.0;HDR=Yes;"";"

    'QuerysQuery = "Select * From [" & Me.vSheet & "$] where [TCID] = 20000 And [Node]

    = 'order management'"

    'Run query against WorkBookoRS.Open sQuery, oConn, 3, 3, 1

    'Move RecordSet to the target RowFor x = 2 to iRow - 1 : oRS.MoveNext : Next

    oDict = CreateObject("Scripting.Dictionary")

    'Use a For..Loop to Build Scripting.DictionaryFor x = 0 to oRS.Fields.Count - 1

    With oDict.Add "" & oRS(x).Name, "" & oRS.Fields(x)

    End With

    NextEnd Function

    Private Property Let oDict(ByVal Val)

    Set mDict = ValEnd PropertyPrivate Property Get oDict()

    Set oDict = mDictEnd Property

    End ClassSet mDataContext = New clsTestData

    sWorkBook = "D:\root\stores\control_files\tc_control_stores.xls" 'WorkBook

    vSheet = "Sheet1" 'WorkSheetiRow = 1 'Row Number

    Set mDict = mDataContext.Load(sWorkBook, vSheet, iRow) 'Took 0.156 seconds to build

    MsgBox mDict("TCID") 'YMsgBox mDict("Priority") 'Scenario 1MsgBox mDict("Node") 'testMsgBox mDict("Channel") 'testMsgBox mDict("Type") 'roundtrip

  • 8/6/2019 Reading Excel

    3/3

    MsgBox mDict("Browser") '1MsgBox mDict("Env")