google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

[求助] 如何读取Excel文档数据问题

本主题由 fishy 于 2008-5-23 16:50 移动

如何读取Excel文档数据问题


Sub Main
    Dim Result As Integer
    dim excel as Object
    dim book as Object
    dim worksheet as Object
    dim s_name as String
    dim s_pass as String
    dim count as Integer
   
    Set excel = GetObject(,"excel.application")
    if(excel Is Nothing) then
        Set excel = CreateObject("excel.application")
        if(excel Is Nothing) then
            MsgBox "Couldn't find Excel!"
            Exit Sub
        End if
    End if
    Set book = excel.Workbooks.Open("c:\Your Book1.xls")
    Set worksheet = book.Worksheet("Sheet1")   
    For count=1 To 2
    s_name = worksheet.Cells(1,count).value
    s_pass = worksheet.Cells(2,count).value
    Next
end sub
请问各位有经验人事,为什么单步调试到红色语句(Set worksheet = book.Worksheet("Sheet1"))时,提示"no such property or method"... ...难道是语法不对吗?不太明白。

[ 本帖最后由 bb64844866 于 2008-5-21 08:55 编辑 ]

TOP

把你的:Set worksheet = book.Worksheet("Sheet1")    这一句改成
Set worksheet = book.Worksheets("Sheet1") 这个吧

TOP

问题已解决,感谢回复者!


漏掉了个字母s,惭愧...感谢二楼朋友。
我想再问下,如果是向Excel中写入数据时,为什么只能写入到第一列呢?如何写到其它列?请指教!
代码如下:
Dim x as Integer
dim book as object
dim worksheet as object
dim excel as object
dim a1
   Open "C:\aa.xls" for output as #1
   For x=1 to 10
      Write #1, x
   Next
   Close #1

TOP

 
当前时区 GMT+8, 现在时间是 2008-7-9 17:52Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹