Microsoft KB Archive/278976

= How To Use XSL to Transform Excel XML Spreadsheet for Server-Side Use =

Article ID: 278976

Article Last Modified on 1/29/2007

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Excel 2002 Standard Edition

-



This article was previously published under Q278976



SUMMARY
Excel 2002 introduces new XML functionality that enables developers to extract formatted data from worksheets in an XML Spreadsheet (XMLSS) format. The Value property of a Range object can return cell formatting and contents in XMLSS. XMLSS is well-formed XML that can be loaded by the Microsoft XML Parser for manipulation at runtime. One potential use of this feature is to post XML data to a server application for processing. This article illustrates one such example, in which an Excel workbook is used as an interface for an Active Server Pages (ASP) script that processes XML data that is extracted from a range in a workbook.

This article guides you through the creation of an Excel workbook, an ASP script, and a stylesheet that all work together as an order processing system. Throughout the steps, explanations of each component and how it contributes to the whole solution are given. The sample described in this article is also available for download; for download instructions, see the &quot;Download&quot; section at the end of this article.



MORE INFORMATION
In this sample, several different components work together to function as an order processor:


 * ASP Script. The server-side component is an ASP script that takes client requests for new orders. The script expects requests to be in a specific XML format. The script takes information from the XML that it receives and adds a new order to the Northwind sample database by using ActiveX Data Objects (ADO).
 * Excel Workbook. The client-side component is an Excel workbook that contains a worksheet for order entry and a macro that interacts with the ASP script to process the user's new order.
 * XSL Stylesheet. A stylesheet transforms the Excel XMLSS to the custom XML format that the ASP script requires. The Excel macro loads the stylesheet and transforms the XMLSS to the custom XML prior to posting the data to the ASP script on the Web server.

Step 1: Create the ASP Order Processing Script
Create a new folder named OrderProc in the virtual root folder of your Web server (the default root folder is C:\Inetpub\Wwwroot). In the OrderProc folder, create a new file named OrderEntry.asp with the script below. The script uses the sample Northwind Access database; you may need to modify the path to Northwind.mdb in the connection string (sConn) to match your Office installation. <%@ Language=&quot;vbscript&quot; CodePage=&quot;65001&quot;%>

<%   Response.Buffer = True Response.ContentType = &quot;text/xml&quot; Dim oDataXML                   'Custom Data XML passed in by caller. Dim oConn                      'ADO Connection to Northwind database. Dim oOrdersRS, oDetailsRS      'ADO Recordsets for the Orders table and Order Details Table. Dim oItems                     'Collection of nodes meeting the match. &quot;Order/Items/Item&quot; Dim oItem                      'Single node in oItems. Dim sCustID                    'Customer ID for the new order. Dim sOrderID                   'Order ID of the newly created record. in Orders table Dim sStatus                    'Status of order processing. Dim bContinue                  'Flag that indicates whether or not to continue processing the order.

Const sConn = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\program files\microsoft office\office10\samples\northwind.mdb&quot; On Error Resume Next

'Load the XML passed into the request. Set oDataXML = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;) oDataXML.Load Request bContinue = True

'Obtain the Customer ID. If no customer id is provided, report an error. sCustID = oDataXML.selectSingleNode(&quot;Order/CustomerID&quot;).Text If sCustID=&quot;&quot; Then sStatus = &quot;There is no customer specified for the order&quot; bContinue = False End If

'Obtain collection of &quot;items&quot; for this order. If the item count = 0, report an error. If bContinue Then Set oItems = oDataXML.SelectNodes(&quot;Order/Items/Item&quot;) if oItems.length = 0 Then sStatus = &quot;There are no items to process for this order&quot; bContinue = False End If   End If

'Open a connection to the Northwind database. If bContinue Then Set oConn = CreateObject(&quot;ADODB.Connection&quot;) oConn.Open sConn if err.Number <> 0 Then sStatus = err.Description bContinue = False end if

End If

'Open the Orders and Order Details tables and add the new records. If bContinue Then

Set oOrdersRS = CreateObject(&quot;ADODB.Recordset&quot;) oOrdersRS.Open &quot;SELECT * FROM Orders&quot;, oConn, 2, 3 Set oDetailsRS = CreateObject(&quot;ADODB.Recordset&quot;) oDetailsRS.Open &quot;SELECT * FROM [Order Details]&quot;, oConn, 2, 3

'Add a new entry in the Orders table. oOrdersRS.AddNew oOrdersRS.Fields(&quot;CustomerID&quot;).Value = sCustID oOrdersRS.Fields(&quot;OrderDate&quot;).Value = CDate(Now) oOrdersRS.Update sOrderID = oOrdersRS.Fields(&quot;OrderID&quot;).Value 'And a new record for each item in the order XML to the Order Details table. If err.number = 0 Then For Each oItem In oItems oDetailsRS.AddNew oDetailsRS.Fields(&quot;OrderID&quot;).Value = sOrderID oDetailsRS.Fields(&quot;ProductID&quot;).Value = oItem.childnodes(0).Text oDetailsRS.Fields(&quot;Quantity&quot;).Value = CLng(oItem.childnodes(1).Text) oDetailsRS.Fields(&quot;UnitPrice&quot;).Value = CLng(oItem.childnodes(2).Text) oDetailsRS.Fields(&quot;Discount&quot;).Value = 0 oDetailsRS.Update Next End If

if err.Number <> 0 Then sStatus = err.Description else sStatus = &quot;Success&quot; end if

'Close the recordsets and connection. oDetailsRS.Close oOrdersRS.Close oConn.Close

End If   'Return the resulting XML (the Order status). Dim sResult sResult = &quot;&quot; sResult = sResult & &quot;&quot; sResult = sResult & &quot;&quot; & sStatus & &quot;&quot; sResult = sResult & &quot;&quot; & sOrderID & &quot;&quot; sResult = sResult & &quot;&quot; Response.Write sResult Response.End %> To successfully process an order request, this ASP script expects XML data structured as follows:

  BOTTM  <Item> <ProductID>4</ProductID> <Quantity>11</Quantity> <Price>20.25</Price> </Item> <Item> <ProductID>18</ProductID> <Quantity>2</Quantity> <Price>63.7</Price> </Item> </Items> </Order>

This XML designates an order for the customer with the ID of &quot;BOTTM&quot;. The order contains two items: 11 units of the product with the ID of 4, and 2 units of the product with the ID of 18.

The ASP script does some error handling to ensure that clients have provided it with valid order information. Clients must provide a Customer ID and at least one item; if these criteria are not met, the ASP script does not process the order and returns an error.

The ASP script returns XML data to clients that have made an order request. This XML indicates the success or failure of the order processing and also provides the order number on success:

<pre class="fixed_text"><?xml version=&quot;1.0&quot;?>  Success</Status> 11078</OrderID> </OrderProcessed>

Step 2: Create the Workbook Interface for Order Entry
<ol> <li>In Excel, start a new workbook.</li> <li> Enter labels in cells A1, A3, B3, and C3 as follows: <pre class="fixed_text">A1:  Customer ID    B1:                C1: A2:                 B2:                C2: A3:  Product ID     B3:   Quantity     C3:   Price </li> <li>Select cells A1:B1. On the Insert menu, point to Name and then click Create. In the Create Names dialog box, select Left Column and click OK. This creates the defined name Customer_ID for cell B1.</li> <li>Select cells A3:C8. On the Insert menu, point to Name and then click Create. In the Create Names dialog box, select Top Row and click OK. This creates the defined names Product_ID, Quantity and Price for cells A4:A8, B4:B8, and C4:C8 respectively.</li> <li>Press ALT+F11 to start the Visual Basic Editor.</li> <li> In the Visual Basic Editor, on the Insert menu, click Module. Add the following macro to the code module: Sub ProcessOrder

Const sFolder = &quot;http://YourWebServer/OrdrProc/&quot;

'Load a new DOMDocument based on the XMLSS of the range A1:C8. Dim oRangeXML Set oRangeXML = CreateObject(&quot;Microsoft.XMLDOM&quot;) oRangeXML.LoadXML Range(&quot;A1:C8&quot;).Value(xlRangeValueXMLSpreadsheet) 'Transform the XMLSS to custom XML that the ASP can 'interpret as a new &quot;order&quot;. Dim oXSL, oOrderXML Set oXSL = CreateObject(&quot;Microsoft.XMLDOM&quot;) oXSL.Load ThisWorkbook.Path & &quot;\OrderEntry.xsl&quot; Set oOrderXML = CreateObject(&quot;Microsoft.XMLDOM&quot;) oRangeXML.transformNodeToObject oXSL, oOrderXML 'Submit the XMLSS to the ASP page for processing. Set oXMLHTTP = CreateObject(&quot;Microsoft.XMLHTTP&quot;) oXMLHTTP.Open &quot;Post&quot;, sFolder & &quot;/OrderEntry.asp&quot;, False oXMLHTTP.setRequestHeader &quot;Content-Type&quot;, &quot;text/xml; charset=&quot;&quot;UTF-8&quot;&quot;&quot; oXMLHTTP.send oOrderXML 'Retrieve the results of the processing by the ASP page. Dim oResult Set oResult = CreateObject(&quot;Microsoft.XMLDOM&quot;) oResult.Load oXMLHTTP.responseXML

'Check the returned XML -- if the Status is &quot;Success&quot;, fill in the 'Order # and display a message. If the Status is not &quot;Success&quot;, 'report the error. Dim sStatus As String sStatus = oResult.selectsinglenode(&quot;OrderProcessed/Status&quot;).Text If sStatus = &quot;Success&quot; Then MsgBox &quot;Thank you. Your order number is &quot; & _ oResult.selectsinglenode(&quot;OrderProcessed/OrderID&quot;).Text Else MsgBox sStatus End If

End Sub NOTE: Change YourWebServer in the sFolder constant to the name of your Web server.

</li> <li>Close the Visual Basic Editor and return to Excel.</li> <li>Save the workbook in the OrdrProc folder you previously created as Invoice1.xls.</li></ol>

The &quot;order form&quot; is essentially cells A1:C8. The macro retrieves the XMLSS for the order form and loads it into a new DOMDocument object. It then loads the Extensible Stylesheet Language (XSL) stylesheet into another DOMDocument and transforms the XMLSS into an XML structure that the ASP can interpret as a new order. The macro uses the XMLHTTP object to post the order XML to the ASP script on the Web server. The ASP script processes the order and then returns more XML to the macro for order status information.

NOTE: As a design choice, the task of transforming the XMLSS to custom Order XML is given to the client-side macro code. You can also post the XMLSS to the ASP script and let ASP perform the transformation for you.

Step 3: Create the Stylesheet
In the OrdrProc folder, create a new file named OrderEntry.xsl and paste the following XSL code: <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/TR/WD-xsl&quot;>

<xsl:template match=&quot;/&quot;> <xsl:pi name=&quot;xml&quot;>version=&quot;1.0&quot;</xsl:pi>

<Order> <xsl:value-of select=&quot;Workbook/Worksheet/Table/Row/Cell[NamedCell[@ss:Name='Customer_ID'] $and$ Data[@ss:Type!='Error']]&quot;/></CustomerID> <Items> <xsl:for-each select=&quot;Workbook/Worksheet/Table/Row[Cell[NamedCell[@ss:Name='Product_ID'] $and$ Data[@ss:Type!='Error']]]&quot;> <Item> <xsl:apply-templates/> </Item> </xsl:for-each> </Items> </Order>

</xsl:template>

<xsl:template match=&quot;Cell[NamedCell[@ss:Name='Product_ID']]&quot;> <ProductID><xsl:value-of select=&quot;Data&quot;/></ProductID> </xsl:template>

<xsl:template match=&quot;Cell[NamedCell[@ss:Name='Quantity']]&quot;> <Quantity><xsl:value-of select=&quot;Data&quot;/></Quantity> </xsl:template>

<xsl:template match=&quot;Cell[NamedCell[@ss:Name='Price']]&quot;> <Price><xsl:value-of select=&quot;Data&quot;/></Price> </xsl:template>

</xsl:stylesheet> The XSL transforms the XMLSS to the custom Order XML (illustrated in Step 1) as follows:


 * The XSL locates the Customer ID by traversing the node hierarchy until it encounters a <Cell> node that contains the <NamedCell> and <Data> child nodes. It checks that <NamedCell> has an ss:Name attribute equal to 'Customer_ID' and that <Data> has an ss:Type attribute that does not equal 'Error'.
 * To locate items for the order, the XSL finds all <Row> nodes with a <Cell> node that contain a <NamedCell> child node with an ss:Name attribute of 'Product_ID' and a <Data> child node with an ss:Type attribute that is not 'Error'.
 * For each <Row> that the XSL determines is an item for the order, the XSL uses templates to match <NamedCell> nodes that have an ss:Name attribute of 'Product_ID', 'Quantity' and 'Price'.

Step 4: Run the Sample Code to Process a New Order
<ol> <li>Return to Invoice1.xls in Excel.</li> <li> To simulate order entry, update Sheet1 with customer and product information as shown below: <pre class="fixed_text">A1:  Customer ID    B1:   BOTTM        C1: A2:                 B2:                C2: A3:  Product ID     B3:   Quantity     C3:   Price A4:  4              B4:   11           C4:   20.25 A5:  18             B5:   2            C5:   63.70 NOTE: For this sample, you must enter valid customer and product IDs for the ASP script to successfully process the order.

</li> <li>On the Tools menu, point to Macro and click Macros. Select the ProcessOrder macro in the list and click Run.</li> <li>If the ASP script successfully processes the order, you receive a message with the new order number. Start Microsoft Access and open the Northwind sample database. You should see a new entry in the Orders table with the Order ID returned from the ASP. You also see two new entries in the Order Details table for the same Order ID. Quit Access and return to Invoice1.xls in Excel.</li> <li>Clear the Customer ID in cell B1 and run the macro again. This time, the server is unable to process the order and it returns an error indicating that no Customer ID has been provided.</li></ol>

Download
ExcelXML.exe contains the sample described in this article as well as an enhanced version of Invoice1.xls. Invoice2.xls uses the same ASP script and XSL stylesheet as Invoice1.xls. However, Invoice2.xls demonstrates additional Excel features, such as worksheet protection, data validation and VLOOKUP formulas, that you can use to provide a more user-friendly order form.

The following file is available for download from the Microsoft Download Center:

Excelxml.exe

Release Date: April 3, 2001

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

<div class="references_section">