PHPExcel:正在分析具有X命名空间的XLSX文件


PHPExcel : Parsing XLSX files with X namespace

我正在使用PHPExcel的最新主机(最近一次提交时间为2015年3月28日)为客户端解析xlsx文件,以下代码读取文件时没有抱怨,但没有发现任何数据(我当前获得的唯一输出为NULL):

<?php
    include 'PHPExcel/IOFactory.php';
    $inputFileName = 'foo.xlsx';
    try {
        $objReader = PHPExcel_IOFactory::createReaderForFile($inputFileName); 
        $objPHPExcel = $objReader->load($inputFileName); 
    } catch (Exception $e) {
        print_r('Error loading file "' . pathinfo($inputFileName, 
                PATHINFO_BASENAME) . '": ' . $e->getMessage());
    }
    $sheet = $objPHPExcel->getSheetByName("Orders");
    var_dump($sheet);           
?>

xlsx文件中的工作簿定义如下所示:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>    
    <x:workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
        <x:fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4506" />
        <x:workbookPr defaultThemeVersion="124226" />
        <x:bookViews>
            <x:workbookView xWindow="480" yWindow="345" windowWidth="24495" windowHeight="11955" />
        </x:bookViews>
        <x:sheets>
            <x:sheet name="Orders" sheetId="4" r:id="relId4" />
        </x:sheets>
        <x:calcPr calcId="125725" />
</x:workbook>

我认为没有"Orders"工作表是不可行的,因为它在默认名称空间中寻找"工作表",而不是"x"名称空间。如果这里的术语有点胡言乱语,我很抱歉,但希望你能明白我的意思。

工作表中肯定有数据,因为我可以在LibreOffice中打开文件并查看它

有没有一种方法可以让PHPExcel使用x命名空间作为默认名称空间,或者以其他方式加载以这种方式定义的工作表?

在我寻找答案的过程中,我发现了一份20101年的错误报告,涉及一个类似的问题,但似乎没有得到解决。

此问题可在https://github.com/PHPOffice/PHPExcel/issues/571

下面是一个变通方法,它很难看,但在这个问题得到解决之前可能会有所帮助。。。

创建一个新类PHPExcel_Reader_Excel2007_XNamespace.php:

<?php
class PHPExcel_Reader_Excel2007_XNamespace extends PHPExcel_Reader_Excel2007
{
    public function securityScan($xml)
    {
        $xml = parent::securityScan($xml);
        return str_replace(['<x:', '</x:'], ['<', '</'], $xml);
    }
}

然后使用这个阅读器加载您的excel文件:

$excelReader = new PHPExcel_Reader_Excel2007_XNamespace();
$objPHPExcel = $excelReader->load($inputFileName);

更新:处理非标准名称空间的一个更一般的问题已经提交,更新的解决方法(也包括"d"名称空间)可以在https://github.com/PHPOffice/PHPExcel/issues/1187#issuecomment-295032648