0

我有一段代码是

    $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。任何帮助将不胜感激谢谢

4

2 回答 2

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;
                            }
                    }
                }
        }
    }
于 2014-05-29T21:04:30.750 回答
0

尝试像这样导入 PHPExcel:

spl_autoload_unregister(array('YiiBase', 'autoload'));
Yii::import('application.vendors.PHPExcel', true);
$this->objPHPExcel = new PHPExcel();
spl_autoload_register(array('YiiBase', 'autoload'));
于 2013-02-04T11:41:56.620 回答