1

我正在使用 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 文件。

4

0 回答 0