How to read excel file and display data in Yii 2?

The Coder picture The Coder · Feb 7, 2015 · Viewed 14.9k times · Source

I have tried to use PHPExcel library to read the excel file and display data from the file. But it was not working for me.

I have also tried using yii2-phpexcel extension also other some ways but nothing worked for me. I hope I would get help from the expert personnel.

I have used PHPExcel in CakePHP to read data from excel but I have no idea how to make code workable in Yii 2. How can I load PHPExcel in Yii2? I am using XLSX. In CakePHP, I can use-

App::import('Vendor', 'Excel/reader'); 
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read(WWW_ROOT . 'uploads/' . $_FILES["filename"]["name"]);
$cells = $data->sheets[0]['cells'];

Thanks in advance.

Answer

Christian Lescuyer picture Christian Lescuyer · Feb 25, 2015

Install phpoffice/phpexcel extension with composer :

composer require phpoffice/phpexcel

In your view :

$objPHPExcel = \PHPExcel_IOFactory::load('./test.xlsx');
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
print_r($sheetData);