Frågan#1
Har för mig att det går att använda excel som en databas att läsa data ur.
Hur kopplar man upp sig mot en excelfil? Använder man vanliga sql frågor?
4 svar · 564 visningar · startad av Mattias Nordin
Har för mig att det går att använda excel som en databas att läsa data ur.
Hur kopplar man upp sig mot en excelfil? Använder man vanliga sql frågor?
Kul att det är möjligt. Men jag får ett fel med nedan...
<%
' Selected constants from adovbs.inc
Const adOpenStatic = 3
Const adLockPessimistic = 2
Dim cnnExcel
Dim rstExcel
Dim I
Dim iCols
' This is all standard ADO except for the connection string.
' You can also use a DSN instead, but so it'll run out of the
' box on your machine I'm using the string instead.
Set cnnExcel = Server.CreateObject("ADODB.Connection")
[b]cnnExcel.Open "DBQ=" & Server.MapPath("derivat.xls") & ";" & _[/b]
"DRIVER={Microsoft Excel Driver (*.xls)};"
' Same as any other data source.
' FYI: TestData is my named range in the Excel file
Set rstExcel = Server.CreateObject("ADODB.Recordset")
rstExcel.Open "SELECT * FROM TestData;", cnnExcel, _
adOpenStatic, adLockPessimistic
' Get a count of the fields and subtract one since we start
' counting from 0.
iCols = rstExcel.Fields.Count
%>
<table border="1">
<thead>
<%
' Show the names that are contained in the first row
' of the named range. Make sure you include them in
' your range when you create it.
For I = 0 To iCols - 1
Response.Write "<th>"
Response.Write rstExcel.Fields.Item(I).Name
Response.Write "</th>" & vbCrLf
Next 'I
%>
</thead>
<%
rstExcel.MoveFirst
' Loop through the data rows showing data in an HTML table.
Do While Not rstExcel.EOF
Response.Write "<tr>" & vbCrLf
For I = 0 To iCols - 1
Response.Write "<td>"
Response.Write rstExcel.Fields.Item(I).Value
Response.Write "</td>" & vbCrLf
Next 'I
Response.Write "</tr>" & vbCrLf
rstExcel.MoveNext
Loop
%>
</table>
<%
rstExcel.Close
Set rstExcel = Nothing
cnnExcel.Close
Set cnnExcel = Nothing
%>
ger för mig...
Microsoft OLE DB Provider for ODBC Drivers error '80040e21'
ODBC driver does not support the requested properties.
/tab_products/sales_marketing/crm_related_info/create_derivat_table/create_table.asp, line 28
Följande fungerar...
<%
Dim objConn
Dim objRS
Set objConn = CreateObject("ADODB.Connection")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\servernsLokalaSökväg\book1.xls ;Extended Properties=Excel 8.0;"
'or could use -> objConn.Open "DBQ=C:\Data\Book1.xls;DRIVER={Microsoft Excel Driver (*.xls)};"
'With Excel & ADO, you work with the sheet as a table
Set objRS = objConn.Execute("select * from [Sheet1$]") %>
<table border=1>
<% while not objRS.eof %>
<tr>
<% For X = 0 To objRS.Fields.Count - 1 %>
<td>
<%=objRS.Fields.Item(X).Name%><br>
</td>
<% Next %>
</tr>
<% objRs.MoveNext
Wend
objRs.Close %>
</table>
<% Set objRS = Nothing
objConn.Close
Set objConn = Nothing %>
Det enda som jag inte förstår är varför det första fältet alltid får bokstaven F framför datan i exceldokumentet. Hmm....
<%Dim objConn
Dim objRS
Set objConn = CreateObject("ADODB.Connection")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\webroot\blaj\book1.xls ;Extended Properties=Excel 8.0;"
'or could use -> objConn.Open "DBQ=C:\Data\Book1.xls;DRIVER={Microsoft Excel Driver (*.xls)};"
'With Excel & ADO, you work with the sheet as a table
Set objRS = objConn.Execute("select * from [Sheet1$]") %>
<table border=1>
<%'*** SKAPAR HEADERN ***' %>
<tr>
<% For X = 0 To objRS.Fields.Count - 1 %>
<td>
<%=objRS.Fields.Item(X).name%><br>
</td>
<% Next %>
</tr>
<%'*** SKAPAR INNEHÅLLET ***' %>
<% while not objRs.eof %>
<tr>
<% For X = 0 To objRS.Fields.Count - 1 %>
<td>
<%=objRS.Fields.Item(X)%><br>
</td>
<% Next %>
</tr>
<%objRs.MoveNext
Wend %>
</table>
<% objRs.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing %>