How to read xls file with phpexcel package? in symfony2

The phpExcel package provides a path for writing xls files, but I do not know how to read the xls file with this package. The bundle can be found at the following address: http://knpbundles.com/liuggio/ExcelBundle

thanks for the help

+6
source share
4 answers

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

The 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; 
+5
source

ExcelBundle is just a wrapper service around the PHPExcel class.

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

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

you can use this as a PHPExcel element to read a file:

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

If you go to the github page in ExcelBundle , you will find exactly the example you are looking for:

If you want to read xls

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

+1
source

Reading a file is like creating a file

create an object starting with the xls file:

 $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject('origin.xls'); // work on the object adding contents // then write to a file $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5'); $writer->save('dest.xls'); 
+1
source

It looks like it just uses the PHPExcel library. You can find examples and documents on this website.

0
source

Source: https://habr.com/ru/post/909300/


All Articles