Yii:: Read Excel File xlsx Format

Posted: July 4, 2011 in Php, Yii Framework

Php & Yii :: Read Excel File xlsx Format

The basic is from here http://www.yiiframework.com/extension/phpexcel-codeplex/

 

Installation

To read the excel xlsx format:

            Yii::import('application.vendors.PHPExcel',true);
            $objReader = PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($file); //$file --> your filepath and filename
            
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow(); // e.g. 10
            $highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
            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";
Comments
  1. ajay says:

    I have a piece of code that is

    $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 = $objReader->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow();
    $hightestColumn = $objWorksheet->getHightestColumn();
    $hightestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

    echo ”.”\n” ;
    for($row=2; $row<$highestRow ; ++$row){
    echo '’.”\n” ;
    for($col=0; $col < $highestColumnIndex ; ++$col){
    echo '’.$objWorksheet->getCellByColumnAndRow($col,$row)->getValue().”.”\n”;
    }
    echo ”.”\n” ;
    }
    echo ”.”\n” ;
    But when this code execute I do not get any file display. I also tried to put debugs to check where it failed, I could localize the error to getActiveSheet. Any help would be appreciated Thanks

  2. sanjeev says:

    thanks a lot , its work !!🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s