2011-04-04 54 views
0

我試圖從Excel電子表格導入XML數據(保存爲'XML Spreadsheet 2003')。 PHP腳本讀取excel數據,然後將數據轉換爲外部XML文件。這個新文件反過來被javscript讀取,以使用谷歌地圖API在谷歌地圖上放置標記。我知道這樣做效率低下,但目標是爲客戶提供一個電子表格,他們可以輕鬆編輯和上傳,並在網站上反映出這些更改,而無需任何干預。創建XML的PHP​​插入額外的標籤和無關數據。幫幫我?

的問題如下:

  • 腳本複製最後的Excel 進入兩次(創建3個XML輸入相同的數據的 )
  • 其中包含數字 有多餘的零放置在細胞 中間的數字(excel是這樣的)
  • excel文件的標題行是
    被包含在XML中,儘管我嘗試了 否則
  • 的XML 的URL屬性應該是空白的,如果它不是 提供,但XML讀「網址」

Excel的屏幕截圖顯示的數據是如何進入:
糾正,沒有圖片noobs喜歡我。點擊屏幕截圖:screenshot

的Excel電子表格XML源(我包括這一切的情況下,我失去了一些東西):

<?xml version="1.0"?> 
<?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"> 
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> 
    <Author>Brandon</Author> 
    <LastAuthor>Brandon</LastAuthor> 
    <Created>2011-04-01T21:05:56Z</Created> 
    <Version>14.00</Version> 
</DocumentProperties> 
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> 
    <AllowPNG/> 
</OfficeDocumentSettings> 
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> 
    <WindowHeight>7995</WindowHeight> 
    <WindowWidth>20115</WindowWidth> 
    <WindowTopX>240</WindowTopX> 
    <WindowTopY>75</WindowTopY> 
    <ProtectStructure>False</ProtectStructure> 
    <ProtectWindows>False</ProtectWindows> 
</ExcelWorkbook> 
<Styles> 
    <Style ss:ID="Default" ss:Name="Normal"> 
    <Alignment ss:Vertical="Bottom"/> 
    <Borders/> 
    <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> 
    <Interior/> 
    <NumberFormat/> 
    <Protection/> 
    </Style> 
    <Style ss:ID="s62"> 
    <Font ss:FontName="Arial" x:Family="Swiss" ss:Bold="1"/> 
    </Style> 
</Styles> 
<Worksheet ss:Name="Sheet1"> 
    <Table ss:ExpandedColumnCount="9" ss:ExpandedRowCount="4" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultRowHeight="15"> 
    <Column ss:AutoFitWidth="0" ss:Width="86.25"/> 
    <Column ss:AutoFitWidth="0" ss:Width="128.25"/> 
    <Column ss:AutoFitWidth="0" ss:Width="156.75"/> 
    <Column ss:AutoFitWidth="0" ss:Width="112.5"/> 
    <Column ss:AutoFitWidth="0" ss:Width="103.5"/> 
    <Row ss:AutoFitHeight="0"> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Lat</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Long</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Name</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Phone</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Address</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">City</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">State</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Zip</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">URL</Data></Cell> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="Number">39.769831000000003</Data></Cell> 
    <Cell><Data ss:Type="Number">-104.972657</Data></Cell> 
    <Cell><Data ss:Type="String">Generic Liquors</Data></Cell> 
    <Cell><Data ss:Type="String">555-123-5555</Data></Cell> 
    <Cell><Data ss:Type="String">42 Walnut St</Data></Cell> 
    <Cell><Data ss:Type="String">Denver</Data></Cell> 
    <Cell><Data ss:Type="String">CO</Data></Cell> 
    <Cell><Data ss:Type="Number">80207</Data></Cell> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="Number">39.763903999999997</Data></Cell> 
    <Cell><Data ss:Type="Number">-104.966311</Data></Cell> 
    <Cell><Data ss:Type="String">Fancy Restaurant</Data></Cell> 
    <Cell><Data ss:Type="String">555-123-5556</Data></Cell> 
    <Cell><Data ss:Type="String">55 Colfax Drive</Data></Cell> 
    <Cell><Data ss:Type="String">Denver</Data></Cell> 
    <Cell><Data ss:Type="String">CO</Data></Cell> 
    <Cell><Data ss:Type="Number">80207</Data></Cell> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="Number">39.759087000000001</Data></Cell> 
    <Cell><Data ss:Type="Number">-104.982963</Data></Cell> 
    <Cell><Data ss:Type="String">Even Fancier Restaurant</Data></Cell> 
    <Cell><Data ss:Type="String">555-123-5557</Data></Cell> 
    <Cell><Data ss:Type="String">1129 Boggio St</Data></Cell> 
    <Cell><Data ss:Type="String">Denver</Data></Cell> 
    <Cell><Data ss:Type="String">CO</Data></Cell> 
    <Cell><Data ss:Type="Number">87505</Data></Cell> 
    </Row> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.3"/> 
    <Footer x:Margin="0.3"/> 
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> 
    </PageSetup> 
    <Unsynced/> 
    <Selected/> 
    <Panes> 
    <Pane> 
    <Number>3</Number> 
    <ActiveRow>4</ActiveRow> 
    <ActiveCol>4</ActiveCol> 
    </Pane> 
    </Panes> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    </WorksheetOptions> 
</Worksheet> 
<Worksheet ss:Name="Sheet2"> 
    <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultRowHeight="15"> 
    <Row ss:AutoFitHeight="0"/> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.3"/> 
    <Footer x:Margin="0.3"/> 
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> 
    </PageSetup> 
    <Unsynced/> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    </WorksheetOptions> 
</Worksheet> 
<Worksheet ss:Name="Sheet3"> 
    <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultRowHeight="15"> 
    <Row ss:AutoFitHeight="0"/> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.3"/> 
    <Footer x:Margin="0.3"/> 
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> 
    </PageSetup> 
    <Unsynced/> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    </WorksheetOptions> 
</Worksheet> 
</Workbook> 
只是相關單元數據的

的Excel XML源:

<Row ss:AutoFitHeight="0"> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Lat</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Long</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Name</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Phone</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Address</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">City</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">State</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Zip</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">URL</Data></Cell> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="Number">39.769831000000003</Data></Cell> 
    <Cell><Data ss:Type="Number">-104.972657</Data></Cell> 
    <Cell><Data ss:Type="String">Generic Liquors</Data></Cell> 
    <Cell><Data ss:Type="String">555-123-5555</Data></Cell> 
    <Cell><Data ss:Type="String">42 Walnut St</Data></Cell> 
    <Cell><Data ss:Type="String">Denver</Data></Cell> 
    <Cell><Data ss:Type="String">CO</Data></Cell> 
    <Cell><Data ss:Type="Number">80207</Data></Cell> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="Number">39.763903999999997</Data></Cell> 
    <Cell><Data ss:Type="Number">-104.966311</Data></Cell> 
    <Cell><Data ss:Type="String">Fancy Restaurant</Data></Cell> 
    <Cell><Data ss:Type="String">555-123-5556</Data></Cell> 
    <Cell><Data ss:Type="String">55 Colfax Drive</Data></Cell> 
    <Cell><Data ss:Type="String">Denver</Data></Cell> 
    <Cell><Data ss:Type="String">CO</Data></Cell> 
    <Cell><Data ss:Type="Number">80207</Data></Cell> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="Number">39.759087000000001</Data></Cell> 
    <Cell><Data ss:Type="Number">-104.982963</Data></Cell> 
    <Cell><Data ss:Type="String">Even Fancier Restaurant</Data></Cell> 
    <Cell><Data ss:Type="String">555-123-5557</Data></Cell> 
    <Cell><Data ss:Type="String">1129 Boggio St</Data></Cell> 
    <Cell><Data ss:Type="String">Denver</Data></Cell> 
    <Cell><Data ss:Type="String">CO</Data></Cell> 
    <Cell><Data ss:Type="Number">87505</Data></Cell> 
    </Row> 

PHP腳本:在markers.xml網絡的

<?php 
// 
// Read the Excel File 
    $data = array(); 
    //format the array values 
    function add_marker($lat, $long, $name, $phone, $address, $city, $state, $zip, $url) 
    { 
    global $data; 

    $data []= array(
    'Lat' => $lat, 
    'Long' => $long, 
    'Name' => $name, 
    'Phone' => $phone, 
    'Address' => $address, 
    'City' => $city, 
    'State' => $state, 
    'Zip' => $zip, 
    'URL' => $url 
    ); 
    } 
    // Load the spreadsheet 
    $dom = DOMDocument::load('/home/public/xml/tour.xml'); 
    // Select XML tag in the spreadsheet to use 
    $rows = $dom->getElementsByTagName('Row'); 
    $first_row = true; 
    foreach ($rows as $row) 
    { 
    if (!$first_row) 
    { 
    $lat = ""; 
    $long = ""; 
    $name = ""; 
    $phone = ""; 
    $address = ""; 
    $city = ""; 
    $state = ""; 
    $zip = ""; 
    $url = ""; 

    $index = 1; 
    $cells = $row->getElementsByTagName('Cell'); 
    foreach($cells as $cell) 
    { 
    $ind = $cell->getAttribute('Index'); 
    if ($ind != null) $index = $ind; 

    if ($index == 1) $lat = $cell->nodeValue; 
    if ($index == 2) $long = $cell->nodeValue; 
    if ($index == 3) $name = $cell->nodeValue; 
    if ($index == 4) $phone = $cell->nodeValue; 
    if ($index == 5) $address = $cell->nodeValue; 
    if ($index == 6) $city = $cell->nodeValue; 
    if ($index == 7) $state = $cell->nodeValue; 
    if ($index == 8) $zip = $cell->nodeValue; 
    if ($index == 9) $url = $cell->nodeValue; 

    $index += 1; 
    } 
    add_marker($lat, $long, $name, $phone, $address, $city, $state, $zip, $url); 
    } 
    $first_row = false; 
    } 
// 
// Write and save xml file 
// 
    //create document 
    $doc = new DOMDocument('1.0', 'UTF-8'); 
    //pretty formatting 
    $doc->formatOutput = true; 

    //create 'markers' root element 
    $root = $doc->createElement('markers'); 
    $doc->appendChild($root); 

    //run through the array constructed from excel file 
    foreach($data as $row) 
    { 
     //create individual marker element 
     $root_child = $doc->createElement('marker'); 
     $root->appendChild($root_child); 

     //set attribute of lat 
     $root_attr1 = $doc->createAttribute('lat'); 
     $root_child->appendChild($root_attr1); 
     //assign 'lat' attribute it's value from array 
     $root_text = $doc->createTextNode($row['Lat']); 
     $root_attr1->appendChild($root_text); 

     //set attribute of lng 
     $root_attr2= $doc->createAttribute('lng'); 
     $root_child->appendChild($root_attr2); 
     //assign 'lng' attribute it's value from array 
     $root_text = $doc->createTextNode($row['Long']); 
     $root_attr2->appendChild($root_text); 

     //set attribute of name 
     $root_attr3= $doc->createAttribute('name'); 
     $root_child->appendChild($root_attr3); 
     //assign 'name' attribute it's value from array 
     $root_text = $doc->createTextNode($row['Name']); 
     $root_attr3->appendChild($root_text); 

     //set attribute of phone 
     $root_attr4= $doc->createAttribute('phone'); 
     $root_child->appendChild($root_attr4); 
     //assign 'phone' attribute it's value from array 
     $root_text = $doc->createTextNode($row['Phone']); 
     $root_attr4->appendChild($root_text); 

     //set attribute of address 
     $root_attr5= $doc->createAttribute('address'); 
     $root_child->appendChild($root_attr5); 
     //assign 'address' attribute it's value from array 
     $root_text = $doc->createTextNode($row['Address']); 
     $root_attr5->appendChild($root_text); 

     //set attribute of city 
     $root_attr6= $doc->createAttribute('city'); 
     $root_child->appendChild($root_attr6); 
     //assign 'city' attribute it's value from array 
     $root_text = $doc->createTextNode($row['City']); 
     $root_attr6->appendChild($root_text); 

     //set attribute of state 
     $root_attr7= $doc->createAttribute('state'); 
     $root_child->appendChild($root_attr7); 
     //assign 'state' attribute it's value from array 
     $root_text = $doc->createTextNode($row['State']); 
     $root_attr7->appendChild($root_text); 

     //set attribute of zip 
     $root_attr8= $doc->createAttribute('zip'); 
     $root_child->appendChild($root_attr8); 
     //assign 'zip' attribute it's value from array 
     $root_text = $doc->createTextNode($row['Zip']); 
     $root_attr8->appendChild($root_text); 

     //set attribute of url 
     $root_attr9= $doc->createAttribute('url'); 
     $root_child->appendChild($root_attr9); 
     //assign 'name' attribute it's value from array 
     $root_text = $doc->createTextNode($row['URL']); 
     $root_attr9->appendChild($root_text); 
    } 

    $doc->save("/home/public/xml/markers.xml");//save("/home/public/xml/markers.xml"); 
?> 

輸出le:

<markers> 
    <marker lat="Lat" lng="Long" name="Name" phone="Phone" address="Address" city="City" state="State" zip="Zip" url="URL"/> 
    <marker lat="39.769831000000003" lng="-104.972657" name="Generic Liquors" phone="555-123-5555" address="42 Walnut St" city="Denver" state="CO" zip="80207" url="URL"/> 
    <marker lat="39.763903999999997" lng="-104.966311" name="Fancy Restaurant" phone="555-123-5556" address="55 Colfax Drive" city="Denver" state="CO" zip="80207" url="URL"/> 
    <marker lat="39.759087000000001" lng="-104.982963" name="Even Fancier Restaurant" phone="555-123-5557" address="1129 Boggio St" city="Denver" state="CO" zip="87505" url="URL"/> 
    <marker lat="39.759087000000001" lng="-104.982963" name="Even Fancier Restaurant" phone="555-123-5557" address="1129 Boggio St" city="Denver" state="CO" zip="87505" url="URL"/> 
    <marker lat="39.759087000000001" lng="-104.982963" name="Even Fancier Restaurant" phone="555-123-5557" address="1129 Boggio St" city="Denver" state="CO" zip="87505" url="URL"/> 
</markers> 

很多額外的數字和額外的標記在那裏。想法?

回答

1

問題1:

腳本複製過去的Excel中錄入兩次(創建相同數據的3個XML條目)

可能與事實是有第二個工作表中的空白行。僅僅因爲您仍在調用add_marker(),即使行中沒有單元格,也會將代碼限制爲第一個工作表中的行。如果你不restrrict只是第一個工作表,那麼你可以做

if ($index > 1) 
    add_marker($lat, $long, $name, $phone, $address, $city, $state, $zip, $url); 

,而不是僅僅

add_marker($lat, $long, $name, $phone, $address, $city, $state, $zip, $url); 

問題2:

包含號碼細胞在數字中間放置了多餘的零(excel是這樣)

更改提取此信息的行以將數據四捨五入到大概6位小數位 改變

$lat = $cell->nodeValue; 

$lat = round($cell->nodeValue,6); 

問題3:

被包含在XML中的Excel文件的標題行,儘管我試圖用其他方式

確保標題實際上是第一張工作表的第1行,以及您提供的代碼應正常工作。如果標題在第2行或以上,或者如果您在工作簿中有任何以前的工作表,那麼您將遇到此問題。

+0

謝謝馬克!工作表很簡單,但這些東西總是被我看着。我刪除了Excel表格中的其他工作表,解決了額外的標記條目,以及第一個包含所有空白標題的標記。令人驚喜的是,URL atrribute現在正確地保留爲空白。舍入功能也起到了一定的作用。再次感謝。 – Brandon 2011-04-04 21:48:31