Laravel-excel: Editing existing xlsx file and exporting it

Created on 12 Feb 2015  路  8Comments  路  Source: Maatwebsite/Laravel-Excel

Hi there

I got an issue where I want to use a template merge it with some data and export it to a new file but I keep getting this error...

Argument 1 passed to Maatwebsite\Excel\Classes\LaravelExcelWorksheet::cloneParent() must be an instance of PHPExcel_Worksheet, null given

My code is:

    $data = [
    [
        'name'  => 'Black Lace',
        'count' => 22
    ],
    [
        'name'  => 'Barry White',
        'count' => 52
    ],
    [
        'name'  => 'The Hoff',
        'count' => 27
    ]
];

$this->excel->load( public_path( 'form.xlsx' ), function ( $file )
{
       $file->sheet( 'sheet1', function ( $sheet )
    {
        $sheet->setCellValue( 'D6', '22' );
    } );
} )->export( 'my-shiny-new-excel-file' );

Am I doing it wrong?

Most helpful comment

I'm having same issues on v1.0.3 with latest l4.
Had to fallback to $sheet = $reader->getExcel()->getActiveSheet(); to handle cells.

All 8 comments

It should work, so it could be something inside your template which is different than the examples I tried it with. If you could give me the template file, I can have a look.

The export method doesn't expect the filename btw, but only the extension (xls, csv).

I'm having same issues on v1.0.3 with latest l4.
Had to fallback to $sheet = $reader->getExcel()->getActiveSheet(); to handle cells.

@rauanmaemirov Thanks for sharing the solution, just ran into it too and it worked!

Wouldn't suggest using a outdated version like 1.0.3, which doesnt support editing templates yet. This was only added in 1.2.*

My bad, should have been clearer. I am using current version, it's the $sheet = $reader->getExcel()->getActiveSheet(); line that made it work. I too don't suggest downgrading or using out-of-date library.

Something you have to checkout (all people above), if the name of the sheet you are calling is matching with your template. Alternativly you can pass through the sheet ID.

I don't get the above error ( it seem like the error happen if you using sheet name that not exist), but can't write to the file if using closure, if I write it directly with chain method , example $excel->sheet('2014')->rows(arrays) like this will solve the writing issue.

Next release will have some existing file modifying improvements.

Was this page helpful?
0 / 5 - 0 ratings