Error reading Excel file using Spout

6.7k views Asked by At

I am having trouble using the spout library in kohana 3.2 to import a big Excel file into mysql. These are the installation steps I have taken:

In the portal file index.php, I added this line of code:

require_once APPPATH.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.'kohana'.DIRECTORY_SEPARATOR.'Spout'.DIRECTORY_SEPARATOR."Autoloader/autoload.php";

This is my reader method:

public static function reader($filename){

    $result = array();

    $reader = ReaderFactory::create(Type::XLSX);
    $reader->open($filename);


    while ($reader->hasNextRow()) {
        $row = $reader->nextRow();
        $code = $row[1];
        $result[] = $code;
    }

    return $result;
}

When I try calling the method, this error message appears:

ERROR: ErrorException [ 1 ]: Class 'ReaderFactory' not found ~ APPPATH/classes/kohana/spoutexcel.php [ 70 ]
2015-09-25 14:29:10 --- STRACE: ErrorException [ 1 ]: Class 'ReaderFactory' not found ~ APPPATH/classes/kohana/spoutexcel.php [ 70 ]

What am I doing wrong?

Spout can be found here: https://github.com/box/spout

2

There are 2 answers

0
mrBrown On

Did you define the namespace?

use Box\Spout\Reader\ReaderFactory;

0
Adrien On

How did you install Spout? If you used Composer, you'll need to include (require_once) the autoload.php file generated by composer when you installed Spout.

If you did not use Composer, you can follow the instructions here: https://github.com/box/spout#manual-installation. It explains which file you need to include to autoload Spout classes.

And as @mrBrown mentioned, don't forget to define the namespaces, by using use Box\Spout\Reader\ReaderFactory;