如何使用PHPExcel生成xml

时间:2015-07-24 10:52:27

标签: arrays xml phpexcel import-from-excel phpexcelreader

我正在使用PHPEXcel库将Excel文件(.xls,.xlsx)转换为XML。 我写了下面的代码将Excel文件转换为数组。但是发现如何以XML格式转换该数组的问题。

 <?php    
        set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');

        include 'PHPExcel/IOFactory.php';

        $inputFileType = 'Excel2007';
        $inputFileNames = array('./sampleData/SamsoniteAU.xlsx');

        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $inputFileName = array_shift($inputFileNames);
        $objPHPExcel = $objReader->load($inputFileName);
        $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));

        echo '<hr />';

        $loadedSheetNames = $objPHPExcel->getSheetNames();
        foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
            $objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
            $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
            print '<pre>';
            print_r($sheetData);
            print '</pre>';
            echo '<br /><br />';
        }
        ?>

create_header函数在$ sheetData数组中包含我想要的XML文件中的xml格式。

带有虚拟数据的XML格式:

function create_header() {
                $header = '<?xml version="1.0" encoding="UTF-8"?>
            <catalog xmlns="http://www.test.com/xml/impex/catalog/2006-10-31" catalog-id="apparel-catalog">
                <product product-id="25421872">
                    <display-name xml:lang="x-default">Laptop Backpack</display-name>
                    <short-description xml:lang="x-default">Short-description</short-description>
                    <long-description xml:lang="x-default">This stylish new range is the ideal convergence between casual and contemporary. These backpacks are lightweight and ultra hard-wearing. With multiple compartments for organisation and a padded laptop protection, this practical range meets everyday business needs.</long-description>
                    <online-flag>true</online-flag>
                    <brand>test</brand>
                    <page-attributes>
                        <page-title xml:lang="x-default">Laptop Backpack</page-title>
                        <page-description xml:lang="x-default">Shop Laptop Backpack in the Official Online Store. Fast Free Standard Delivery.</page-description>

                    </page-attributes>
                </product>
            </catalog>';
                return $header;
            }

数组如下所示。我的Excel可能有任何列的多个值。

[1] => Array
        (
            [A] => product-id
            [B] => display-name
            [C] => brand
            [D] => short-description
            [E] => long-description
            [F] => page-title
            [G] => page-description
        )

    [2] => Array
        (
            [A] => 25421872
            [B] => Laptop Backpack
            [C] => test
            [D] => >Short-description
            [E] => This stylish new range is the ideal convergence between casual and contemporary. These backpacks are lightweight and ultra hard-wearing. With multiple compartments for organisation and a padded laptop protection, this practical range meets everyday business needs.
            [F] => Laptop Backpack
            [G] => Shop Laptop Backpack in the Official Online Store. Fast Free Standard Delivery.

        )

请帮助我可以使用哪个PHPExcel_writer从数组生成XML文件。

0 个答案:

没有答案