(need explanation) spout reader documentation

3.3k views Asked by At

can someone help me with this spout documentation,

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

$reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');

$reader->open($filePath);

foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $row) {
        // do stuff with the row
        $cells = $row->getCells();
        ...
    }
}

$reader->close();

what i don't get is :

  1. What should i do with ('/path/to/file.ext')
  2. where do the $filePath come from ? do we need to create a variable named $filePath which will directing to the temporary uploaded file ?

I have read the docimentation and i still don't get it I also tried to implement it with code igniter, here's my code :

require_once APPPATH.'third_party\spout\src\Spout\Autoloader\autoload.php'; use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

class Excel extends CI_Controller {

//==========================================================
// C O N S T R U C T O R
//==========================================================
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Excel_model');
    }

    public function index()
    {
        $reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');
        $filePath = APPPATH.'third_party\spout\src\temp\test.xlsx';
        $reader->open($filePath);

        foreach ($reader->getSheetIterator() as $sheet) {
            foreach ($sheet->getRowIterator() as $row) {
                // do stuff with the row
                $cells = $row->getCells();
                echo $cells;
            }
        }

        $reader->close();   
    }   

}   

and got this error message instead :

An uncaught Exception was encountered

Type: Box\Spout\Common\Exception\UnsupportedTypeException

Message: No readers supporting the given type: ext

Filename: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderFactory.php

Line Number: 59

Backtrace:

File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderFactory.php
Line: 42
Function: createFromType

File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderEntityFactory.php
Line: 24
Function: createFromFile

File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\controllers\Excel.php
Line: 20
Function: createReaderFromFile

File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\index.php
Line: 315
Function: require_once
1

There are 1 answers

2
Adrien On BEST ANSWER

File path is a string pointing to the spreadsheet you're trying to read. You need to define it and pass it to Spout this way:

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

$filePath = APPPATH.'third_party\spout\src\temp\test.xlsx';
$reader = ReaderEntityFactory::createReaderFromFile($filePath);
$reader->open($filePath);

foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $row) {
        // do stuff with the row
        $cells = $row->getCells();
        ...
    }
}

$reader->close();

Can you give it a try?