概要 您可以为用户提供在 Microsoft Excel 中显示数据的选择,而不在 HTML 表中显示 Web 数据。本文演示如何使用从 Microsoft SQL Server 获取的表格式数据创建 Web 页,并通过将 Web 页内容与 Excel 中的 MIME 类型相关联,在浏览器中以 Excel 方式呈现该页。
更多信息 要执行这些步骤并测试结果,您需要有: Microsoft Windows NT Server 4.0, SP3 Microsoft Internet Information Server (IIS) 4.0 版或更高版本 Microsoft SQL Server 6.5 或更高版本 Microsoft Excel 97、Excel 2000 和 Excel 2002
分步过程 使用 ODBC 控制面板小程序创建一个名为“pubs”的系统 DSN。将 DSN 设置为引用您的本地 SQL Server,使用 SQL Server 身份验证,并使用“pubs”作为默认数据库。pubs 数据库的默认登录 ID 是 sa,没有密码。 使用 Notepad.exe 在您的 IIS 主目录(例如 inetpubwwwroot)中创建一个名为 XlTest.asp 的文件,并向其中添加以下内容:
〈%@ Language=VBScript %〉 〈% ’Change HTML header to specify Excel’s MIME content type Response.Buffer = TRUE Response.ContentType = “application/vnd.ms-excel“ %〉 〈HTML〉 〈BODY〉 Here is the info you requested.〈p〉 〈% ’ Create ADO Connection object dim myConnection set myConnection = CreateObject(“ADODB.Connection“) ’ Open SQL Server Pubs database... ’ myConnection.Open “DSN=pubs;UID=sa“ ’ Get a recordset of info from Authors table... sqlStr = “SELECT au_fname,au_lname,phone FROM authors“ set rsAuthors = myConnection.Execute(sqlStr) %〉 〈!-- Our table which will be translated into an Excel spreadsheet --〉 〈TABLE WIDTH=75% BORDER=1 CELLSPACING=1 CELLPADDING=1〉 〈TR〉 〈TD〉〈font size=+2〉First Name〈/font〉〈/TD〉 〈TD〉〈font size=+2〉Last Name〈/font〉〈/TD〉 〈TD〉〈font size=+2〉Phone〈/font〉〈/TD〉 〈/TR〉 〈!-- server-side loop adding Table entries --〉 〈% do while not rsAuthors.EOF %〉 〈TR〉 〈TD〉〈%=rsAuthors(“au_fname“)%〉〈/TD〉 〈TD〉〈%=rsAuthors(“au_lname“)%〉〈/TD〉 〈TD〉〈%=rsAuthors(“phone“)%〉〈/TD〉 〈/TR〉 〈% rsAuthors.MoveNext loop ’ Clean up rsAuthors.Close set rsAuthors = Nothing myConnection.Close set myConnection = Nothing %〉 〈/TABLE〉 〈/BODY〉 〈/HTML〉
保存 XlTest.asp,然后从客户端浏览器查看它。例如,http://我的服务器/XlTest.asp(使用您的服务器名称替换我的服务器。)
如果客户机上安装了 Microsoft Excel 97,Microsoft Excel 将在浏览器中显示该数据。
|