Reading Excel with ADO

K

KPS

Hello,

I have tried to read Excel spreadsheet through ADO. I used the sample code
provided by msdn.I don't know what the **** is the matter with the code
cause I can't read the Excel. I tried similar system with Access and it
works fine. Here is the error and the sample code.

Error:

Microsoft OLE DB Provider for ODBC Drivers error '80040e37'

[Microsoft][ODBC Excel Driver] The Microsoft Jet database engine could not
find the object 'ASPTOC'. Make sure the object exists and that you spell its
name and the path name correctly.

/ASPTOC.asp, line 21

Code:

<%@Language=VBScript %>
<html>
<head>
<title> Displaying An Excel Spreadsheet in an Web Page </title>
</head>
<body bgcolor="#FFFFFF" text="#000000" >
<h1>ASP Table of Contents</h1>
<%
'Creates an instance of an Active Server Component
Set oConn = Server.CreateObject("ADODB.Connection")
'Connects to the Excel driver and the Excel spreadsheet
'in the directory where the spreadsheet was saved
strProvider = "Driver={Microsoft Excel Driver (*.xls)};
DBQ=C:\Inetpub\wwwroot\Testi_site\ASPTOC.xls;"
'Opens the connection to the data store
oConn.Open strProvider
'Selects the records from the Excel spreadsheet
strCmd = "SELECT * from `ASPTOC`"
Set oRS = Server.CreateObject("ADODB.Recordset")
'Opens the recordset
oRS.Open strCmd, oConn
'Prints the cells and rows in the table
Response.Write "<table border=1><tr><td>"
'Gets records in spreadsheet as a string and prints them in the table
Response.Write oRS.GetString (, , "</tr><td>", "</td></tr><tr><td>",
NBSPACE)
%>
</body>
</html>

It looks like ODBC error but I did the ODBC like instructed. I also
installed the .net framework and latest MDAC to the server. The sample code
instructions say that u should use Excel 98 or 2000. I'm using Excel 2002.
Can this be the problem?? I think i've tried everything but can't figure
this out. Can anyone please help me???

Thanks!

KS
 
P

Paul Clement

¤ Hello,
¤
¤ I have tried to read Excel spreadsheet through ADO. I used the sample code
¤ provided by msdn.I don't know what the **** is the matter with the code
¤ cause I can't read the Excel. I tried similar system with Access and it
¤ works fine. Here is the error and the sample code.
¤
¤ Error:
¤
¤ Microsoft OLE DB Provider for ODBC Drivers error '80040e37'
¤
¤ [Microsoft][ODBC Excel Driver] The Microsoft Jet database engine could not
¤ find the object 'ASPTOC'. Make sure the object exists and that you spell its
¤ name and the path name correctly.
¤
¤ /ASPTOC.asp, line 21
¤
¤ Code:
¤
¤ <%@Language=VBScript %>
¤ <html>
¤ <head>
¤ <title> Displaying An Excel Spreadsheet in an Web Page </title>
¤ </head>
¤ <body bgcolor="#FFFFFF" text="#000000" >
¤ <h1>ASP Table of Contents</h1>
¤ <%
¤ 'Creates an instance of an Active Server Component
¤ Set oConn = Server.CreateObject("ADODB.Connection")
¤ 'Connects to the Excel driver and the Excel spreadsheet
¤ 'in the directory where the spreadsheet was saved
¤ strProvider = "Driver={Microsoft Excel Driver (*.xls)};
¤ DBQ=C:\Inetpub\wwwroot\Testi_site\ASPTOC.xls;"
¤ 'Opens the connection to the data store
¤ oConn.Open strProvider
¤ 'Selects the records from the Excel spreadsheet
¤ strCmd = "SELECT * from `ASPTOC`"
¤ Set oRS = Server.CreateObject("ADODB.Recordset")
¤ 'Opens the recordset
¤ oRS.Open strCmd, oConn
¤ 'Prints the cells and rows in the table
¤ Response.Write "<table border=1><tr><td>"
¤ 'Gets records in spreadsheet as a string and prints them in the table
¤ Response.Write oRS.GetString (, , "</tr><td>", "</td></tr><tr><td>",
¤ NBSPACE)
¤ %>
¤ </body>
¤ </html>
¤
¤ It looks like ODBC error but I did the ODBC like instructed. I also
¤ installed the .net framework and latest MDAC to the server. The sample code
¤ instructions say that u should use Excel 98 or 2000. I'm using Excel 2002.
¤ Can this be the problem?? I think i've tried everything but can't figure
¤ this out. Can anyone please help me???

The problem is probably with the worksheet reference in your SQL statement but I would use Jet OLEDB
instead of ODBC in any event:

Dim oConn
Dim rs

Set oConn = Server.CreateObject("ADODB.Connection")

oConn .Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\Book10.xls;Extended Properties=Excel 8.0"

Set oRS = Server.CreateObject("ADODB.Recordset")

rs.Open "Select * from [Sheet1$]", oConn


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top