Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read from Excel using VBScript

Posted on 2006-11-03
4
Medium Priority
?
20,314 Views
Last Modified: 2008-03-10
I use the following VBScript function to read data from Excel.
It works fine, but I have to specify the spreadsheet name (sSQL = "SELECT * FROM [" & sSheetName & "$]").
Is there a way to read from Excel without specifying spreadsheet name?

For example now I call the function

ReadDateFromExcel("c:\myfile.xls", "Sheet1")

I'd like to omit the spreadsheet name (Sheet1) and just read from the first spreadsheet in Excel document, regardless what it's named

ReadDateFromExcel("c:\myfile.xls")



Function ReadDataFromExcel(sFileNameAndPath, sSheetName)
      Dim conn
      Set conn = CreateObject("ADODB.Connection")
      With conn
            .Provider = "Microsoft.Jet.OLEDB.4.0"
            .ConnectionString = _
          "Data Source=" & sFileNameAndPath & ";" & _
          "Extended Properties=Excel 8.0;"
            .Open
      End With
      Dim sSQL,rs,vRows, strtext
      Set rs = CreateObject("ADODB.Recordset")
      sSQL = "SELECT * FROM [" & sSheetName & "$]"

      rs.open sSQL, conn

      i=0
      do while not rs.eof
            for each field in rs.fields
                  if Trim(strtext)="" then
                        strtext=strtext & field.value
                  else
                        strtext=strtext & "      " & field.value
                  end if
            next
            i=i+1
            rs.moveNext()
      loop

      rs.close
      Set rs = nothing
      conn.close
      Set conn = nothing
      vRows=Split(strtext,vbTab)
      ReadDataFromExcel=vRows
End Function
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
WMIF earned 500 total points
ID: 17869973
check out the answer in this question.  it does what you need.
http:Q_21369273.html
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17871150
problem with working with excel files (as opposed to a spreadsheet saved as a csv or txt file) is that it can have multiple worksheets.

I think that code in the linnk WMIF posted is good if you know the worksheet nameas already (well...at least for the accepted answer)  I haven't tried out that FSO method to find the name, but that may be what you need.  Or even further down the post:

dim xlADOConn, rs, strFileName
          strFileName = server.mappath("/myfile.xls")
          set xlADOConn = Server.CreateObject("ADODB.Connection")
          Call xlADOConn.Open("Driver={Microsoft Excel Driver (*.xls)};" & _
            "DriverId=790;" & _
            "Dbq=" & strFileName & ";")
          Set rs = xlADOConn.OpenSchema(20)
          if not rs.eof then
            SheetName =  rs("Table_Name")
          end if
          set rs = nothing
          set xlADOConn = nothing






please no points for me...just pointing and elaborating a little on WMIF's post
0
 
LVL 22

Expert Comment

by:WMIF
ID: 17872279
whoa, that accepted answer is not what i remember reading earlier.  i must not have been paying that good of attention.  sorry about that.

@kevp75 - glad you found something in there to work for him though.
0
 
LVL 35

Author Comment

by:YZlat
ID: 17881737
Thanks! That was exactly what I needed
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question