yii phpexcel文件阅读器没有回复

时间:2013-02-04 08:19:16

标签: yii phpexcel

我有一段代码

    $excelFile = '/tmp/sheet.xls' ;
    Yii::import('application.vendors.PHPExcel',true);
    $inputFileType = PHPExcel_IOFactory::identify($excelFile);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($excelFile);
    $objWorkSheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorkSheet->getHighestRow();
    $hightestColumn = $objWorkSheet->getHightestColumn();
    $hightestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

    echo '<table>'."\n" ;
    for($row=2; $row<$highestRow ; ++$row){
    echo '<tr>'."\n" ;
    for($col=0; $col < $highestColumnIndex ; ++$col){
    echo '<td>'.$objWorksheet->getCellByColumnAndRow($col,$row)->getValue().'</td>'."\n";
    }
    echo '</tr>'."\n" ;
  }    
  echo '</table>'."\n" ;

但是当这段代码执行时,我没有得到任何文件显示。我还尝试调试以检查失败的位置,我可以将错误本地化为getActiveSheet。 任何帮助,将不胜感激 感谢

2 个答案:

答案 0 :(得分:0)

尝试像这样导入PHPExcel:

spl_autoload_unregister(array('YiiBase', 'autoload'));
Yii::import('application.vendors.PHPExcel', true);
$this->objPHPExcel = new PHPExcel();
spl_autoload_register(array('YiiBase', 'autoload'));

答案 1 :(得分:0)

以下是我如何显示导入的excel文件并且有效:

    spl_autoload_unregister(array('YiiBase','autoload'));           
    Yii::import("application.vendors.phpexcel.Classes.PHPExcel", true);

    $inputFileType = PHPExcel_IOFactory::identify($path);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType); //Excel5);
    $objPHPExcel = $objReader->load($path);

    spl_autoload_register(array('YiiBase','autoload'));  

    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        foreach ($worksheet->getRowIterator() as $row) {
                $rowIndex=$row->getRowIndex();
                $cellIterator = $row->getCellIterator();
                $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
                foreach ($cellIterator as $cellIndex=>$cell) {
                    if (!is_null($cell)) {
                        if($rowIndex >1 && $cellIndex<=55){
                            $cellVal=$cell->getCalculatedValue();
                                echo $cellVal;
                            }
                    }
                }
        }
    }