Excel Importer (using PHPExcel)

Version Date Notes By
0.1 2017-09-05 Initial release ROB

Importing information using the PHPExcelImporter class is a two step process. First you generate the template and then you import the filled in template. To generate the template just add the following code inside a controller function (note: the response will be an excel or csv file):

$importer = new PHPExcelImporter(); // init the PHPExcelImporter
$importer
    ->setFormat('csv') // set the format of the template (note: template and imported template must use the same format)
    ->setHeader([ // setup the header titles (the top row will have the translated slugs for each header item)
        'form.field.designation',
        'form.field.contact-person',
        'form.field.email'
    ])
    ->downloadTemplate($request); // execute the download (the function creates the response, so no need to return anything)

Inside a second method of the controller, you will receive the imported (filled in) template:

$importer = new PHPExcelImporter();
$result = $importer
    ->setModel(Addressee::class) // set the model to save to
    ->setFormat('csv') // set the format
    ->setHeader([ // set the field that each column should save to
        'name',
        'contact_person',
        'email'
    ])
    ->addForceValue('addressee_type_id', $request->addressee_type_id) // add aditional fixed values
    ->addForceValue('status_id', 1)
    ->addForceValue('created_at', Carbon::now())
    ->addForceValue('created_by', \Auth::user()->id)
    ->readTemplate($request->import_file); // import the template