Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a xls file with phpexcel bundle? in Symfony2

Tags:

symfony

The bundle phpExcel provide the way for write xls files, but i don't know how read a file xls with this bundle.. the bundle can be found at the following address : http://knpbundles.com/liuggio/ExcelBundle

Thanks for your help

like image 423
jpparedesm Avatar asked Feb 24 '12 19:02

jpparedesm


2 Answers

If you really want to read a EXCEL file you can find an example in the official doc : https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/28iterator.php

Code :

$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
    $file = $this->get('kernel')->getRootDir()."/../web/uploads/import/membres.xlsx";
    if (!file_exists($file)) {
        exit("Please run 05featuredemo.php first." );
    }
    $objPHPExcel = \PHPExcel_IOFactory::load($file);

    echo date('H:i:s') , " Iterate worksheets" , EOL;
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
            echo 'Worksheet - ' , $worksheet->getTitle() , EOL;

            foreach ($worksheet->getRowIterator() as $row) {
                    echo '    Row number - ' , $row->getRowIndex() , EOL;

                    $cellIterator = $row->getCellIterator();
                    $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
                    foreach ($cellIterator as $cell) {
                            if (!is_null($cell)) {
                                    echo '        Cell - ' , $cell->getCoordinate() , ' - ' , $cell->getCalculatedValue() , EOL;
                            }
                    }
            }
    }


    // Echo memory peak usage
    echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
like image 76
jmdu973 Avatar answered Oct 12 '22 17:10

jmdu973


The ExcelBundle is just a wrapper service around the PHPExcel class.

the bundle service will return a PHPExcel_IOFactory with a specific file format expected in input and output (in this case, xls5)

 $xl_obj=$this->get('xls.load_xls5')

you can use that as a PHPExcel item, so to read a file:

  $my_xl_file = $xl_obj->load($fileName);

If you go to the github page of ExcelBundle, there is exactly the example you are looking for:

If you want read xls

$exelObj = $this->get('xls.load_xls5')->load($filename);

like image 26
Roberto Avatar answered Oct 12 '22 17:10

Roberto