2017-07-28 87 views
0

我認爲這將是一個簡單的xml加載和循環,但出於某種原因,我無法從Excel XML電子表格中讀取任何內容。如何在經典ASP中讀取excel xml電子表格數據

Excel電子表格,這似乎是一個標準化的輸出的內容,如下:

<?mso-application progid="Excel.Sheet"?> 
<Workbook 
    xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
    xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:x="urn:schemas-microsoft-com:office:excel" 
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
    xmlns:html="http://www.w3.org/TR/REC-html40" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:user="urn:my-scripts"> 
    <DocumentProperties 
     xmlns="urn:schemas-microsoft-com:office:office">... 
    </DocumentProperties> 
    <OfficeDocumentSettings 
     xmlns="urn:schemas-microsoft-com:office:office">... 
    </OfficeDocumentSettings> 
    <ExcelWorkbook 
     xmlns="urn:schemas-microsoft-com:office:excel">... 
    </ExcelWorkbook> 
    <Styles>...</Styles> 
    <Worksheet ss:Name="Sheet1" ss:Protected="1"> 
     <Names> 
      <NamedRange ss:Name="Print_Titles" ss:RefersTo="=Sheet1!C2,Sheet1!R7"/> 
     </Names> 
     <Table ss:ExpandedColumnCount="34" ss:ExpandedRowCount="32" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="13.2"> 
      <Column ss:Hidden="1" ss:Width="175.2"/> 
      <Column ss:Width="250.20000000000002"/> 
      <Column ss:Width="60" ss:Span="31"/> 
      <Row ss:AutoFitHeight="0" ss:Height="150" ss:StyleID="s67">...</Row> 
      <Row ss:Height="15"> 
       <Cell ss:StyleID="s69"> 
        <Data ss:Type="String">W89231</Data> 
       </Cell> 
       <Cell ss:StyleID="s70"> 
        <Data ss:Type="String">Tom Brown</Data> 
        <NamedCell ss:Name="Print_Titles"/> 
       </Cell> 
       <Cell ss:StyleID="s69"> 
        <Data ss:Type="String">1E+</Data> 
       </Cell> 
       <Cell ss:StyleID="s69"> 
        <Data ss:Type="String">1m</Data> 
       </Cell> 

等等等等

已經讀取byTagName不是我嘗試的最佳方法以下單節點代碼優先:

strFilename = "1B.xml" 
Set oXMLDoc = Server.CreateObject("MSXML2.DOMDocument.6.0") 
oXMLDoc.async = False 
oXMLDoc.Load (Server.MapPath(strFilename)) 
Set NodeList = oXMLDoc.documentElement.selectNodes("Workbook/Worksheet") 
For Each Node In NodeList 
Response.Write Node.selectSingleNode("row/cell/data/text()") 
Next 

但絕對沒有任何反應。

於是我又回到了久經考驗的byTagName和嘗試以下操作:

strFilename = "1B.xml" 
Set oXMLDoc = Server.CreateObject("MSXML2.DOMDocument.6.0") 
oXMLDoc.async = False 
oXMLDoc.Load (Server.MapPath(strFilename)) 
Set oRows = oXMLDoc.documentElement.getElementsByTagName("Row") 
Response.Write oRows.length 

的oRows.length反饋是零

我只是似乎無法進入DOM即使它是一個標準的Excel XML電子表格格式。

我真的很感激,如果有人可以請解釋我如何通過行讀取並提取必要的數據值。

問候

湯姆

+0

湯姆 - 您可能希望將其加載爲文本,包,在類似 ...並嘗試加載,作爲XML到MSXML。它可能與兩個頂級節點混淆。 –

+1

嗨VWombat - 發現問題是所有與名稱空間有關。這部分在網絡上看起來確實很差 –

+1

湯姆 - 那麼爲什麼不寫一個你自己的問題的答案呢。也許這將有助於增加文檔池? –

回答

1

管理,找出它的所有有關的命名空間。

通過添加下面的行:

Set oXMLDoc = Server.CreateObject("MSXML2.DOMDocument.6.0") 
oXMLDoc.setProperty "SelectionLanguage", "XPath" 
oXMLDoc.setProperty "SelectionNamespaces", "xmlns:myns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40' xmlns:msxsl='urn:schemas-microsoft-com:xslt' xmlns:user='urn:my-scripts'" 

我可以然後引用標記在XML中,如下所示:

Set oXMLRows = oXMLDoc.selectNodes("/myns:Workbook/myns:Worksheet/myns:Table/myns:Row") 

,然後將XML解析可以開始!! :0)

問候

湯姆