本文將說明「動態伺服器網頁」(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 指令碼
在 Web 伺服器的虛擬根目錄 (預設根目錄為 c:\inetpub\wwwroot) 中,將下列 ASP 另存為
XMLSS.asp:
<% Language=VBScript %>
<%
Response.Buffer = True
Response.ContentType = "text/xml"
Dim NumOrders, NumProds, r
NumOrders = 300
NumProds = 10
Dim oSS
Dim oOrdersSheet
Dim oTotalsSheet
Dim oRange
Dim c
Set oSS = CreateObject("OWC10.Spreadsheet")
Set c = oSS.Constants
'Rename Sheet1 to "Orders", rename Sheet2 to "Totals" and remove Sheet3
Set 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 formatting
Set oRange = oOrdersSheet.Range("A1:F1")
oRange.Value = Array("Order Number", "Product ID", "Quantity", "Price", "Discount", "Total")
oRange.Font.Bold = True
oRange.Interior.Color = "Silver"
oRange.Borders(c.xlEdgeBottom).Weight = c.xlThick
oRange.HorizontalAlignment = c.xlHAlignCenter
'Apply formatting to the columns
oOrdersSheet.Range("A:A").ColumnWidth = 20
oOrdersSheet.Range("B:E").ColumnWidth = 15
oOrdersSheet.Range("F:F").ColumnWidth = 20
oOrdersSheet.Range("A2:E" & NumOrders + 1 _
).HorizontalAlignment = c.xlHAlignCenter
oOrdersSheet.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 2
Dim aOrderData
aOrderData = GetOrderInfo
oOrdersSheet.Range("A2:E" & NumOrders + 1).Value = aOrderData
'Add a formula to calculate the order total for each row and format the column
oOrdersSheet.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 rows
oOrdersSheet.UsedRange.Borders(c.xlInsideHorizontal).Weight = c.xlThin
oOrdersSheet.UsedRange.BorderAround , c.xlThin, 15
'Turn on AutoFilter and display an initial criteria where
'the Product ID (column 2) is equal to 5
oOrdersSheet.UsedRange.AutoFilter
oOrdersSheet.AutoFilter.Filters(2).Criteria.FilterFunction = c.ssFilterFunctionInclude
oOrdersSheet.AutoFilter.Filters(2).Criteria.Add "5"
oOrdersSheet.AutoFilter.Apply
'Add a Subtotal at the end of the usedrange
oOrdersSheet.Range("F" & NumOrders + 3).Formula = "=SUBTOTAL(9, F2:F" & NumOrders + 1 & ")"
'Apply window settings for the Orders worksheet
oOrdersSheet.Activate 'Makes the Orders sheet active
oSS.Windows(1).ViewableRange = oOrdersSheet.UsedRange.Address
oSS.Windows(1).DisplayRowHeadings = False
oSS.Windows(1).DisplayColumnHeadings = False
oSS.Windows(1).FreezePanes = True
oSS.Windows(1).DisplayGridlines = False
'=== Build the Second Worksheet (Totals) ===========================================
'Change the Column headings and hide row headings
oTotalsSheet.Activate
oSS.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 1
Dim aProductIDs
aProductIDs = GetProductIDs
oTotalsSheet.Range("A1:A" & NumProds).Value = aProductIDs
oTotalsSheet.Range("A1:A" & NumProds).HorizontalAlignment = c.xlHAlignCenter
'Add a formula to column 2 that computes totals per product from the Orders Sheet
oTotalsSheet.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 worksheet
oSS.Windows(1).ViewableRange = oTotalsSheet.UsedRange.Address
'=== Setup for final presentation ==================================================
oSS.DisplayToolbar = False
oSS.AutoFit = True
oOrdersSheet.Activate
Response.Write oSS.XMLData
Response.End
Function GetOrderInfo()
ReDim aOrderInfo(NumOrders,5)
Dim aPrice, aDisc
aPrice = 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-1
aOrderInfo(r, 0) = "'" & String(7-Len(CStr(r+1)), "0") & r+1 'Col 1 is Order Number
aOrderInfo(r, 1) = Int(Rnd() * NumProds) + 1 'Col 2 is Product ID
aOrderInfo(r, 2) = Int(Rnd() * 20) + 1 'Col 3 is Quantity
aOrderInfo(r, 3) = aPrice(aOrderInfo(r, 1)-1) 'Col 4 is Price
aOrderInfo(r, 4) = aDisc(Int(Rnd() * 4)) 'Col 5 is Discount
Next
GetOrderInfo = aOrderInfo
End Function
Function GetProductIDs()
ReDim aPIDs(NumProds, 1)
For r = 0 To NumProds-1
aPIDs(r, 0) = r+1
Next
GetProductIDs = aPIDs
End Function
%>
在網頁上顯示 XMLSS
如果要在網頁上顯示範例 XMLSS,您只需要將試算表元件的
XMLURL 屬性設定為 ASP 的 URL 即可,如下所示:
<html>
<body>
<object classid="clsid:0002E551-0000-0000-C000-000000000046" id="Spreadsheet1">
<param name="XMLURL" value="http://YourWebServer/xmlss.asp">
</object>
</body>
</html>
注意:如果您使用的是 Office 2003,則需視情況變更上述程式碼中的 classid。
在前述的 HTML
中,
XMLURL 屬性是由 <param> 標記所設定。如有需要,您也可以在執行階段設定
XMLURL 屬性:
Spreadsheet1.XMLURL = "http://YourWebServer/xmlss.asp"
在 Microsoft Excel 中顯示 XMLSS
使用試算表元件建立的 XMLSS,可以在 Microsoft Excel 中開啟。您在試算表元件中實作的格式和功能可以與
Microsoft Excel 共用。某些試算表元件可以支援的功能,Excel 並不支援 (反之亦然)。開啟 XMLSS 後,Excel 無法實作的任何
XML 標記或屬性會被忽略。
如果要在 Microsoft Excel 中檢視範例 ASP 指令碼的結果,請依照下列步驟執行:
- 啟動 Microsoft Excel。
- 在 [檔案] 功能表上,按一下 [開啟舊檔]。
- 在 [檔案名稱] 方塊中,輸入
http://YourWebServer/xmlss.asp,然後按一下 [開啟]。
檢查活頁簿,並請注意,在 Excel
中開啟活頁簿時,於執行階段套用的資料和格式全部出現在活頁簿中。唯一的例外為:在試算表元件中建立的標題不會繼續沿用至 Excel,因為這是 Microsoft
Excel 無法共用的試算表元件功能。如果您為了要在 Excel 中顯示檔案而使用試算表元件建立
XMLSS,請留意這兩者所支援的不同功能。
另一種在 Microsoft Excel 中開啟由 ASP 建立之 XMLSS 的方法,是提供
Excel「多用途網際網路郵件延伸標準」(MIME) 類型,做為 ASP 中的
ContentType。當您使用 Excel MIME 類型並瀏覽至 ASP 時,就可以在瀏覽器的 Microsoft Excel 中就地呈現
XMLSS,如下所示:
- 在文字編輯器中開啟 XMLSS.asp。
- 將指令碼中的下列行:
Response.ContentType = "text/xml"
變更為:
Response.ContentType = "application/vnd.ms-excel"
- 將變更儲存至 XMLSS.asp 並啟動 Internet Explorer (IE)。
- 瀏覽至 http://YourWebServer/XMLSS.asp。XML 試算表會就地呈現在瀏覽器所裝載的
Microsoft Excel 中。
如需詳細資訊,請造訪下列 Microsoft 網站提供的 Office Web 元件主題:
如需詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
285891?
(http://support.microsoft.com/kb/285891/
)
如何使用 Visual Basic 或 ASP 在 Excel 2002 和 Excel 2003 中建立 XML 試算表
278976?
(http://support.microsoft.com/kb/278976/
)
How To Use XSL to Transform Excel XML Spreadsheet for Server-Side Use
257757?
(http://support.microsoft.com/kb/257757/
)
INFO:Office 伺服器端自動化的考量