除了“读取excel数据形成html表格”的技术外,你可能有兴趣想了解怎样用程序实现电子数据表和名字范围的下拉菜单列表。
除了单元格的内容,用ado还能知道更多的信息---在上面,已经讨论了用它得到字段名的列表。
调用openschema方法,可以得到当前打开数据库(同样适用于电子数据簿)的特殊记录集,在这里讨论的例程中,是取回了当前电子数据簿的电子数据表和名字范围。在数据库的操作中,通过传递给ado一个adschematablesas命令参数,就可以得到所有表的信息。
set oschemars = oconn.openschema(adschematables)
上述调用将返回一个记录集信息,对于文件theworkbook.xls,结果是:
table_name table_type
employees$ system table
listofproducts$ system table
suppliers$ system table
a_duplicate_name table
alphabetical_list_of_products table
employees table
listofproducts$a_duplicate_name table
product_totals table
和记录集相比较,电子数据工作簿中的电子数据表(worksheets)被当作系统表,名字范围被当作通常表。通常情况下,无名范围(可以使用!)不被记录集报告。
掌握了这些信息,创建字段下拉菜单就成了分离两个表类型和使用合适的标记的工作了。比如,可以执行下面的html/asp代码段来创建工作表列表的下拉菜单:
< select name="xlsheet" >
< %
voptions = "< option >< /option >"
do while not oschemars.eof
if oschemars("table_type") = "system table" then
voptions = voptions & "< option >" & _
server.htmlencode(oschemars("table_name")) & _
"< /option >
end if
oschemars.movenext
loop
response.write voptions
% >
< /select >
实际的readx1.asp代码中可能还复杂一些,因为要处理上一个选项的显示,同时要使用客户端的javascript脚本检查输入的合法性。
