当前位置导航:炫浪网>>网络学院>>网页制作>>ASP教程

如何在ASP中通过ODBC调用Excel中的数据

      在ASP中通过ODBC调用Excel中的数据方式和普通的通过ODBC调用数据库的方式有一些不同的地方。下面将介绍如何通过ODBC在ASP中使用Excel的数据。
    在使用的过程中,要注意的是你需要使用的ODBC驱动为:Microsoft ODBC Driver for Excel.
下面给出一个例子来具体说明如何调用的过程:
1。在Excel的电子表格中新建一个叫Range的表单
a)在Excel文件ADOtest.xls中的sheet1里随便输入下面的测试数据:
        column1                column2                       column3
        rr                     this                          15
        bb                     test                          20
        ee                     works                         25
[b]注意事项[/b]:
       如果你的Excel电子表格中某一列同时包含了文本和数字的话,那么Excel的ODBC驱动将不能够正常
    处理这一行的数据类型,你必须要保证该列的数据类型一致。:
          i.Microsoft OLE DB Provider for ODBC Drivers error '80040e21'
                 The request properties can not be supported by this ODBC Driver.
          ii.Microsoft OLE DB Provider for ODBC Drivers error '80004005'
                 The query is not updateable because it contains no searchable columns to use as a
                 hopeful key.
          iii.Microsoft OLE DB Provider for ODBC Drivers error '80004005'
                 Query based update failed. The row to update could not be found.
b)使用鼠标选中你的数据所在的所有行和列
c)从菜单中选择Insert\Name\Define命令.
d)输入myRange1,然后单击OK退出
[b]注意事项[/b]:
       i)ADO假设Excel中的第一行为字段名.所以你定义的范围中必须要包括第一行的内容
       ii)Excel中的行标题(即字段名)不能够包含数字. Excel的驱动在遇到这种问题时就会出错的。
          例如你的行标题名为“F1”
e)新建一个ODBC系统DSN指向这个ADOTest.xls文件:(这个过程我就不详细说了)
[b]注意事项[/b]:
    i)DSN中要选择Access97版本
    ii)在NT中设置好匿名帐号对该电子表格文件相应的访问权限
    iii)如果你的权限设置不正确的话,有可能会得到如下出错信息:
  Microsoft OLE DB Provider for ODBC Drivers error '80004005'
       [Microsoft][ODBC Excel Driver] The Microsoft Jet database engine
       cannot open the file '(unknown)'. It is already opened exclusively
       by another user, or you need permission to view its data.
f)最后一步,就是在ASP中调用这个ODBC,代码如下:
             <%@ LANGUAGE="VBSCRIPT" %>
             <%
               Set objConn = Server.CreateObject("ADODB.Connection")
               objConn.Open "ADOExcel"

               Set objRS = Server.CreateObject("ADODB.Recordset")
               objRS.ActiveConnection = objConn
               objRS.CursorType = 3                    
               objRS.LockType = 2                      
               objRS.Source = "Select * from myRange1"
               objRS.Open
          %>
          <br>
          <%
             Response.Write("Original Data")
             Response.Write("<TABLE><TR>")
             For X = 0 To objRS.Fields.Count - 1
                Response.Write("<TD>" & objRS.Fields.Item(X).Name & "</TD>")
             Next
             Response.Write("</TR>")
             objRS.MoveFirst
             While Not objRS.EOF
                Response.Write("<TR>")
                For X = 0 To objRS.Fields.Count - 1
                   Response.write("<TD>" & objRS.Fields.Item(X).Value)
                Next
                objRS.MoveNext
                Response.Write("</TR>")
             Wend
             Response.Write("</TABLE>")
             objRS.MoveFirst
             objRS.Fields(0).Value = "change"
             objRS.Fields(1).Value = "look"
             objRS.Fields(2).Value = "30"
             objRS.Update
             Response.Write("<br>Data after the update")
             Response.Write("<TABLE><TR>")
             For X = 0 To objRS.Fields.Count - 1
                Response.Write("<TD>" & objRS.Fields.Item(X).Name & "</TD>")
             Next
             Response.Write("</TR>")
             objRS.MoveFirst
             While Not objRS.EOF
                Response.Write("<TR>")
                For X = 0 To objRS.Fields.Count - 1
                   Response.write("<TD>" & objRS.Fields.Item(X).Value)
                Next
                objRS.MoveNext
                Response.Write("</TR>")
             Wend
             Response.Write("</TABLE>")
             objRS.Close
             Set objRS = Nothing
             objConn.Close
             Set objConn = Nothing
          %>


 
相关内容
赞助商链接