home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / excel_data.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  2.1 KB  |  78 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. ' Selected constants from adovbs.inc
  15. Const adOpenStatic = 3
  16. Const adLockPessimistic = 2
  17.  
  18. Dim cnnExcel
  19. Dim rstExcel
  20. Dim I
  21. Dim iCols
  22.  
  23. ' This is all standard ADO except for the connection string.
  24. ' You can also use a DSN instead, but so it'll run out of the
  25. ' box on your machine I'm using the string instead.
  26. Set cnnExcel = Server.CreateObject("ADODB.Connection")
  27. cnnExcel.Open "DBQ=" & Server.MapPath("xl_data.xls") & ";" & _
  28.     "DRIVER={Microsoft Excel Driver (*.xls)};"
  29.  
  30. ' Same as any other data source.
  31. ' FYI: TestData is my named range in the Excel file
  32. Set rstExcel = Server.CreateObject("ADODB.Recordset")
  33. rstExcel.Open "SELECT * FROM TestData;", cnnExcel, _
  34.     adOpenStatic, adLockPessimistic
  35.  
  36. ' Get a count of the fields and subtract one since we start
  37. ' counting from 0.
  38. iCols = rstExcel.Fields.Count
  39. %>
  40. <table border="1">
  41.     <thead>
  42.         <%
  43.         ' Show the names that are contained in the first row
  44.         ' of the named range.  Make sure you include them in
  45.         ' your range when you create it.
  46.         For I = 0 To iCols - 1
  47.             Response.Write "<th>"
  48.             Response.Write rstExcel.Fields.Item(I).Name
  49.             Response.Write "</th>" & vbCrLf
  50.         Next 'I
  51.         %>
  52.     </thead>
  53.     <%
  54.     rstExcel.MoveFirst
  55.  
  56.     ' Loop through the data rows showing data in an HTML table.
  57.     Do While Not rstExcel.EOF
  58.         Response.Write "<tr>" & vbCrLf
  59.         For I = 0 To iCols - 1
  60.             Response.Write "<td>"
  61.             Response.Write rstExcel.Fields.Item(I).Value
  62.             Response.Write "</td>" & vbCrLf
  63.         Next 'I
  64.         Response.Write "</tr>" & vbCrLf
  65.  
  66.         rstExcel.MoveNext
  67.     Loop
  68.     %>
  69. </table>
  70.  
  71. <%
  72. rstExcel.Close
  73. Set rstExcel = Nothing
  74.  
  75. cnnExcel.Close
  76. Set cnnExcel = Nothing
  77. %>
  78.