如何使用php从xml创建excel文件的正确输出?

时间:2017-11-25 05:03:24

标签: php xml excel

我一直在努力从xml创建一个excel文件。 这是我从其他stackoverflow回答得到的php代码,我尝试使用它:

function createexcel() {
        header("Content-Type: application/vnd.ms-excel");
        header("Content-disposition: attachment; filename=spreadsheet.xlsx");// put your file name here
        $url = 'C:/xampp/htdocs/Ready/xml_product_feed.xml'; // xml file location with file name
        if (file_exists($url)) {
        $xml = simplexml_load_file($url);
        echo 'Thumbnail'."\t".'Product Name'."\t".'Price'."\t".'Stock'."\t".'Description'."\t\n";
        foreach($xml->xpath("//item") as $item)
        {
          $extraData = $item->children('g',true);
          echo $extraData->thumbnail_url."\t".$extraData->productname."\t".$extraData->price."\t".$extraData->avail."\t".$extraData->productdescription."\t"."\t\n";
        }
      }
    }

这是xml文件示例:

<?xml version="1.0" encoding="UTF-8"?>
  <item id="1234">
    <g:productname>Blue Shirt</g:productname>
    <g:productdescription>Height : 70cm 
Width : 60 cm 
Material : cotton</g:productdescription>
    <g:price>12</g:price>
    <g:avail>20</g:avail>
    <g:thumbnail_url>http://matchem.com/wp-content/uploads/2015/03/plain-blue-shirt-front-and-back-72hi3bcb.jpg</g:thumbnail_url>
  </item>

它成功下载了excel文件,但问题是,Description列中的excel文件的值不会放在一列中。 这是它的屏幕截图:

Screen shoot of the excel file

我想将Description值放在一列中。真的需要帮助。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

首先,当我保存您的XML时,它对我无效。

所以我对此进行了更改,如果您的XML工作正常,请不要使用我的XML

<强> XML

<?xml version="1.0" encoding="UTF-8"?>
  <item id="1234">
    <productname>Blue Shirt</productname>
    <productdescription>Height : 70cm Width : 60 cm Material : cotton</productdescription>
    <price>12</price>
    <avail>20</avail>
    <thumbnail_url>http://matchem.com/wp-content/uploads/2015/03/plain-blue-shirt-front-and-back-72hi3bcb.jpg</thumbnail_url>
  </item>

<强> PHP

<?php 
    createexcel();
    function createexcel() {
        header("Content-Type: application/vnd.ms-excel");
        header("Content-disposition: attachment; filename=spreadsheet.xls");// put your file name here
        $url = 'C:/xampp/htdocs/new/xml_file.xml'; // xml file location with file name
        if (file_exists($url)) {
            $xml = simplexml_load_file($url);
            echo 'Thumbnail'."\t".'Product Name'."\t".'Price'."\t".'Stock'."\t".'Description'."\t\n";
            foreach($xml->xpath("//item") as $item)
            echo $item->thumbnail_url."\t".$item->productname."\t".$item->price."\t".$item->avail."\t".$item->productdescription."\t"."\t\n";
        }
    }
?>

您只需要为您的问题更改XML。请进行以下更改

您的XML

            <?xml version="1.0" encoding="UTF-8"?>
      <item id="1234">
        <g:productname>Blue Shirt</g:productname>
        <g:productdescription>Height : 70cm 
    Width : 60 cm 
    Material : cotton</g:productdescription>
        <g:price>12</g:price>
        <g:avail>20</g:avail>
        <g:thumbnail_url>http://matchem.com/wp-content/uploads/2015/03/plain-blue-shirt-front-and-back-72hi3bcb.jpg</g:thumbnail_url>
      </item>   

新XML

    <?xml version="1.0" encoding="UTF-8"?>
    <item id="1234">
        <productname>Blue Shirt</productname>
        <productdescription>Height : 70cm Width : 60 cm Material : cotton</productdescription>//Change Here
        <price>12</price>
        <avail>20</avail>
        <thumbnail_url>http://matchem.com/wp-content/uploads/2015/03/plain-blue-shirt-front-and-back-72hi3bcb.jpg</thumbnail_url>
    </item>