如何使用 ASP 构建在客户端显示的 XML 表格

来源:百度文库 编辑:神马文学网 时间:2024/04/26 19:36:59
察看本文应用于的产品
文章编号 : 288130
最后修改 : 2005年12月6日
修订 : 4.2
概要
本文介绍了 Active Server Pages (ASP) 脚本,这种脚本可通过 Office 电子表格组件以 XML 表格 (XMLSS) 格式构建电子表格。XMLSS 可在客户端通过以下方式之一显示: • 在网页上的电子表格组件中显示。
• 在浏览器内置的 Microsoft Excel 中显示。
• 直接在 Microsoft Excel 中打开。
回到顶端
更多信息
与服务器端 Microsoft Excel自动化相比,使用服务器端代码中的电子表格组件构建电子表格可提供更强的伸缩性和更好的性能。Microsoft 不推荐在服务器上自动化Office 应用程序(包括 Excel),如果使用其他方法可以获得相同结果,应避免这种做法。XMLSS 可以保留电子表格组件和Microsoft Excel 的许多通用功能,例如,含多个工作表的工作簿、单元格格式、自动筛选、单元格公式和重新计算功能。电子表格组件具有与Microsoft Excel 对象模型非常匹配的对象模型。因此,如果您熟悉 Excel 对象模型,则可以轻松应用一些现有的 Excel代码,并进行修改以用于电子表格组件。
以下示例说明了如何使用电子表格组件和 ASP 在 XMLSS 中生成含多个工作表的工作簿。该示例还讨论了如何在网页或 Microsoft Excel 中显示生成的 XMLSS 客户端。使用电子表格组件构建 XMLSS 的 ASP 脚本
将下面的 ASP 另存为 XMLSS.asp,并保存在 Web 服务器的虚拟根目录中(默认的根目录为 c:\inetpub\wwwroot):<% Language=VBScript %><%Response.Buffer = TrueResponse.ContentType = "text/xml"Dim NumOrders, NumProds, rNumOrders = 300NumProds = 10Dim oSSDim oOrdersSheetDim oTotalsSheetDim oRangeDim cSet oSS = CreateObject("OWC10.Spreadsheet")Set c = oSS.Constants‘Rename Sheet1 to "Orders", rename Sheet2 to "Totals" and remove Sheet3Set oOrdersSheet = oSS.Worksheets(1)oOrdersSheet.Name = "Orders"Set oTotalsSheet = oSS.Worksheets(2)oTotalsSheet.Name = "Totals"oSS.Worksheets(3).Delete‘=== Build the First Worksheet (Orders) ==============================================‘Add headings to A1:F1 of the Orders worksheet and apply formattingSet oRange = oOrdersSheet.Range("A1:F1")oRange.Value = Array("Order Number", "Product ID", "Quantity", "Price", "Discount", "Total")oRange.Font.Bold = TrueoRange.Interior.Color = "Silver"oRange.Borders(c.xlEdgeBottom).Weight = c.xlThickoRange.HorizontalAlignment = c.xlHAlignCenter‘Apply formatting to the columnsoOrdersSheet.Range("A:A").ColumnWidth = 20oOrdersSheet.Range("B:E").ColumnWidth = 15oOrdersSheet.Range("F:F").ColumnWidth = 20oOrdersSheet.Range("A2:E" & NumOrders + 1 _).HorizontalAlignment = c.xlHAlignCenteroOrdersSheet.Range("D2:D" & NumOrders + 1).NumberFormat = "0.00"oOrdersSheet.Range("E2:E" & NumOrders + 1).NumberFormat = "0 % "oOrdersSheet.Range("F2:F" & NumOrders + 1).NumberFormat = "$ 0.00" ‘"_($* #,##0.00_)"‘Obtain the order information for the first five columns in the Orders worksheet‘and populate the worksheet with that data starting at row 2Dim aOrderDataaOrderData = GetOrderInfooOrdersSheet.Range("A2:E" & NumOrders + 1).Value = aOrderData‘Add a formula to calculate the order total for each row and format the columnoOrdersSheet.Range("F2:F" & NumOrders + 1).Formula = "=C2*D2*(1-E2)"oOrdersSheet.Range("F2:F" & NumOrders + 1).NumberFormat = "_( $* #,##0.00 _)"‘Apply a border to the used rowsoOrdersSheet.UsedRange.Borders(c.xlInsideHorizontal).Weight = c.xlThinoOrdersSheet.UsedRange.BorderAround , c.xlThin, 15‘Turn on AutoFilter and display an initial criteria where‘the Product ID (column 2) is equal to 5oOrdersSheet.UsedRange.AutoFilteroOrdersSheet.AutoFilter.Filters(2).Criteria.FilterFunction = c.ssFilterFunctionIncludeoOrdersSheet.AutoFilter.Filters(2).Criteria.Add "5"oOrdersSheet.AutoFilter.Apply‘Add a Subtotal at the end of the usedrangeoOrdersSheet.Range("F" & NumOrders + 3).Formula = "=SUBTOTAL(9, F2:F" & NumOrders + 1 & ")"‘Apply window settings for the Orders worksheetoOrdersSheet.Activate ‘Makes the Orders sheet activeoSS.Windows(1).ViewableRange = oOrdersSheet.UsedRange.AddressoSS.Windows(1).DisplayRowHeadings = FalseoSS.Windows(1).DisplayColumnHeadings = FalseoSS.Windows(1).FreezePanes = TrueoSS.Windows(1).DisplayGridlines = False‘=== Build the Second Worksheet (Totals) ===========================================‘Change the Column headings and hide row headingsoTotalsSheet.ActivateoSS.Windows(1).ColumnHeadings(1).Caption = "Product ID"oSS.Windows(1).ColumnHeadings(2).Caption = "Total"oSS.Windows(1).DisplayRowHeadings = False‘Add the product IDs to column 1Dim aProductIDsaProductIDs = GetProductIDsoTotalsSheet.Range("A1:A" & NumProds).Value = aProductIDsoTotalsSheet.Range("A1:A" & NumProds).HorizontalAlignment = c.xlHAlignCenter‘Add a formula to column 2 that computes totals per product from the Orders SheetoTotalsSheet.Range("B1:B" & NumProds).Formula = _"=SUMIF(Orders!B$2:B$" & NumOrders + 1 & ",A1,Orders!F$2:F$" & NumOrders + 1 & ")"oTotalsSheet.Range("B1:B" & NumProds).NumberFormat = "_( $* #,##0.00 _)"‘Apply window settings for the Totals worksheetoSS.Windows(1).ViewableRange = oTotalsSheet.UsedRange.Address‘=== Setup for final presentation ==================================================oSS.DisplayToolbar = FalseoSS.AutoFit = TrueoOrdersSheet.ActivateResponse.Write oSS.XMLDataResponse.EndFunction GetOrderInfo()ReDim aOrderInfo(NumOrders,5)Dim aPrice, aDiscaPrice = Array(10.25, 9.5, 2.34, 6.57, 9.87, 4.55, 6, 13.05, 3.3, 5.5)aDisc = Array(0, 0.1, 0.15, 0.2)For r = 0 To NumOrders-1aOrderInfo(r, 0) = "‘" & String(7-Len(CStr(r+1)), "0") & r+1 ‘Col 1 is Order NumberaOrderInfo(r, 1) = Int(Rnd() * NumProds) + 1 ‘Col 2 is Product IDaOrderInfo(r, 2) = Int(Rnd() * 20) + 1 ‘Col 3 is QuantityaOrderInfo(r, 3) = aPrice(aOrderInfo(r, 1)-1) ‘Col 4 is PriceaOrderInfo(r, 4) = aDisc(Int(Rnd() * 4)) ‘Col 5 is DiscountNextGetOrderInfo = aOrderInfoEnd FunctionFunction GetProductIDs()ReDim aPIDs(NumProds, 1)For r = 0 To NumProds-1aPIDs(r, 0) = r+1NextGetProductIDs = aPIDsEnd Function%>在网页上显示 XMLSS
要在网页上显示示例 XMLSS,只需将电子表格组件的 XMLURL 属性设置为 ASP 的 URL,如下所示:
注意:如果使用的是 Office 2003,则必须根据需要更改以上代码中的 classid。
在上述 HTML 中,使用 标记设置 XMLURL 属性。如果需要,还可以在运行时设置 XMLURL 属性: Spreadsheet1.XMLURL = "http://YourWebServer/xmlss.asp"在 Microsoft Excel 中显示 XMLSS
使用电子表格组件创建的 XMLSS 可以在 Microsoft Excel 中打开。在电子表格组件中实现的格式和功能可与 MicrosoftExcel 共享。有一些功能是电子表格组件支持而 Excel 不支持的(或 Excel 支持而电子表格组件不支持);Excel 未实现的任何XML 标记或属性在打开 XMLSS 时都将被忽略。
要在 Microsoft Excel 中查看示例 ASP 脚本的结果,请执行下列操作: 1. 启动 Microsoft Excel。
2. 在文件菜单上,单击打开。
3. 在文件名框中,键入 http://YourWebServer/xmlss.asp,然后单击打开。
检查工作簿,在 Excel中打开该工作簿时您会注意到,运行时应用的数据和格式都显示在工作簿中。有一个例外情况:在电子表格组件中创建的标题不会带入Excel,因为这是电子表格组件的功能,而 Microsoft Excel 中不提供该功能。如果使用电子表格组件创建 XMLSS 以便在Excel 中显示文件,请注意两者各自支持的不同功能。
在 Microsoft Excel 中打开 ASP 创建的 XMLSS 的另一种方法是,在 ASP 中提供 Excel 多用途 Internet 邮件扩展 (MIME) 类型作为 ContentType。使用 Excel MIME 类型并浏览到 ASP 时,XMLSS 可在浏览器内置的 Microsoft Excel 中显示,如下所示: 1. 在文本编辑器中打开 XMLSS.asp。
2. 在脚本中找到以下行: Response.ContentType = "text/xml" 将其更改为: Response.ContentType = "application/vnd.ms-excel"
3. 保存对 XMLSS.asp 所做的更改并启动 Internet Explorer (IE)。
4. 浏览到 http://YourWebServer/XMLSS.asp。XML 表格将在浏览器内置的 Microsoft Excel 中显示。
回到顶端
参考
有关其他信息,请访问下面的 Microsoft 网站,查看 Office Web Components 主题:http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)
有关其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:285891 (http://support.microsoft.com/kb/285891/)如何使用 Visual Basic 或 ASP 创建 Excel 2002 和 Excel 2003 的 XML 表格
278976 (http://support.microsoft.com/kb/278976/) 如何使用 XSL 转换在服务器端使用的 Excel XML 表格
257757 (http://support.microsoft.com/kb/257757/) 服务器端 Office 自动化应考虑的因素