简体   繁体   English

PHPExcel在xls和xlsx之间切换

[英]PHPExcel switch between xls and xlsx

I am writing a script that reads .xls and .xlsx files from mail attachments. 我正在编写一个从邮件附件中读取.xls和.xlsx文件的脚本。 I'm trying to make the script switch between readers based on the file extension using the identify method. 我正在尝试使用identify方法基于文件扩展名在读者之间切换脚本。

$strSheetNameResult = 'QA Result';
$strSheetNameComments = 'QA Comments'; 

foreach($aPaths as $strPath) {
    try {
        $strFileType = PHPExcel_IOFactory::identify($strPath);
        echo $strFileType;

        $oReader = PHPExcel_IOFactory::createReader($strFileType);

        $oReader->setLoadSheetsOnly($strSheetNameResult);

        $oSheetData = $oReader->load($strPath)->getActiveSheet();

.xls works just fine, but as soon as it encounters an .xlsx file it doesn't use the right reader and gives me an error: `Fatal error: .xls可以正常工作,但是一旦遇到.xlsx文件,它就不会使用正确的阅读器,并给我一个错误:致命错误:

Call to a member function getCell() on a non-object in C:\\xampp\\htdocs\\cronjob\\Test.php on line 37` 在第37行的C:\\ xampp \\ htdocs \\ cronjob \\ Test.php中的非对象上调用成员函数getCell()。

line 37: 第37行:

$aSheetData['strProjectName'] = $oSheetData->getCell('B4')->getValue();

I think this error is caused because it's not using the right reader. 我认为此错误是由于未使用正确的阅读器引起的。

$aPaths : $aPaths

Array(
    [0] => C:\xampp\tmp\105943-632345.xls
    [1] => C:\xampp\tmp\112047-634744.xlsx
    [2] => C:\xampp\tmp\112069-634917.xls
    [3] => C:\xampp\tmp\113840-634955.xls
    [4] => C:\xampp\tmp\115760-635374.xlsx
    [5] => C:\xampp\tmp\120294-637780.xls
    [6] => C:\xampp\tmp\120801-638144.xls
    [7] => C:\xampp\tmp\121098-638118.xls
    [8] => C:\xampp\tmp\124831-641137.xlsx
    [9] => C:\xampp\tmp\127680-642962.xls
    [10] => C:\xampp\tmp\127689-642665.xls
    [11] => C:\xampp\tmp\127692-642784.xls
    [12] => C:\xampp\tmp\127700-643048.xls
    [13] => C:\xampp\tmp\127708-643096.xls
    [14] => C:\xampp\tmp\128771-642241.xls
    [15] => C:\xampp\tmp\129082-647219.xls
    [16] => C:\xampp\tmp\129629-647241.xls
    [17] => C:\xampp\tmp\134488-647334.xls
    [18] => C:\xampp\tmp\134500-646313.xls
    [19] => C:\xampp\tmp\134508-644581.xls
    [20] => C:\xampp\tmp\134511-646521.xls
    [21] => C:\xampp\tmp\134512-646136.xls
    [22] => C:\xampp\tmp\134561-650010.xls

)

Does anybody have an idea why this isn't working, or a different way to switch between readers? 是否有人知道为什么这种方法不起作用,或者有不同的方式在读者之间进行切换?

you can try: 你可以试试:



    $file_types = explode(".", 'C:\xampp\tmp\105943-632345.xls');
    $file_type = strtolower($file_types [count($file_types) - 1]);

    if($file_type=='xls') {
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
    }elseif($file_type=='xlsx'){
        $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    }else{
        echo 'file type error!';
        exit;
    }

There was a corrupt file in the list that identified as an xls file but was an xlsx file. 列表中有一个损坏的文件,标识为xls文件,但它是xlsx文件。 I deleted the file and it works perfectly now! 我删除了文件,现在可以正常使用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM