duanre4421 2016-05-20 07:35
浏览 66
已采纳

PHPExcel在xls和xlsx之间切换

I am writing a script that reads .xls and .xlsx files from mail attachments. I'm trying to make the script switch between readers based on the file extension using the identify method.

$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:

Call to a member function getCell() on a non-object in C:\xampp\htdocs\cronjob\Test.php on line 37`

line 37:

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

I think this error is caused because it's not using the right reader.

$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?

  • 写回答

2条回答 默认 最新

  • duaevb1511 2016-05-20 08:17
    关注

    There was a corrupt file in the list that identified as an xls file but was an xlsx file. I deleted the file and it works perfectly now!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭