Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

squibby's avatar

Laravel Excel - How to add cell to each row?

Hi,

Using Laravel excel package I would like to load an existing csv file and the for each row add some data in custom column/cell

I can load a file like below and I can the target the first sheet. I can then choose a specific cell to update and export.

Excel::load($tempFile, function($doc) {

    $sheet = $doc->setActiveSheetIndex(0);
    $sheet->setCellValue('D5', 'Test');

})->export('csv');

How can I target the end of each row (when I don't know how many columns there will be)

Something like the following...

Excel::load($tempFile, function($doc) {

    $sheet = $doc->setActiveSheetIndex(0);
        foreach($sheet->rows as $row){
            $row->appendCell('Some custom data');
        }

})->export('csv');

Does anyone have any experience doing that?

Thanks.

0 likes
2 replies
InaniELHoussain's avatar

for the time being and according to the doc there is no method to append new cell, you can only append new rows

1 like
squibby's avatar
squibby
OP
Best Answer
Level 8

@InaniELHoussain Yes you are right. To get around this, I have had to grab the contents of the file into a collection , then append the required data then finally creating a new csv export.

2 likes

Please or to participate in this conversation.