|
本帖最后由 yinjiajia0626 于 2014-7-26 17:52 编辑
- '调用D盘的1.txt资源,在setting中
- Class oDataDic
- Private oDic 'DIctionary对象
- Public oWorkBookPath 'xls文件对象
- Public oSheetName
- Public oRowNo 'Rows have number
- Public Default Function Load(oWorkBookPath,oSheetName,oRowNo)
- With Me
- .oWorkBookPath=oWorkBookPath
- .oSheetName=oSheetName
- .oRowNo=oRowNo
- End With
- BuildContext
- Set Load=oDic
- End Function
- Private Function BuildContext
- Dim oConn,oRS,arrData,x
- Set oConn=CreateObject("adodb.Connection")
- Set oRS=CreateObject("adodb.RecordSet")
- oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;"&_
- "Data Source="&Me.oWorkBookPath&";"&_
- "Extended Properties=""Excel 8.0;HDR=Yes;"";"
- sQuery="Select * from ["&Me.oSheetName&"$]"
- oRs.Open sQuery,oConn,3,3,1
- For x=2 to oRowNo -1:oRS.MoveNext:
- Next
- oDict=CreateObject("Scripting.Dictionary")
- For x=0 to oRS.Fields.Count-1
- msgbox oRS.Fields.Item(x)
- With oDict
- .Add "" & oRS(x).Name,"" & oRS.Fields(x)
- End With
- Next
- End Function
- Private Property Let oDict(ByVal val)
- Set oDict=Val
- End Property
- Private Property Get oDict()
- Set oDict = oDic
- End Property
- End Class
- Set mDataContext=New oDataDic
- oWorkBookPath="D:\1.xls"
- oSheetName="Sheet1"
- oRowNo=3
- Set mDict=mDataContext.Load(oWorkBookPath,oSheetName,oRowNo)
- MsgBox mDict("Age")
- MsgBox mDict("Sex")
- MsgBox mDict("Username")
- MsgBox mDict("Password")
- Set mDict=Nothing
复制代码
出错的截图:
附件为我用到的1.xls |
|