LearnProgramming
LearnProgramming

Reputation: 806

Laravel, how to modify/edit excel base on this code?

I have a working code in controller as shown below, problem is that when I wanted to edit its content, it doesn't save.

Controller

$rows = Excel::load($file_path, function($reader) use ($column_number)
{
    $reader->takeColumns($column_number);
    $reader->noHeading();

})->get();

$rows = $rows->toArray();

View Content

dump($rows[0][0]); // this will display A1 cell and it is working fine

But when I try to do like this

$rows[0][0] = "Test"; // this will not write the cell A1
dump($rows); // but it will display as shown in picture below

enter image description here

Upvotes: 1

Views: 1136

Answers (2)

irfanengineer
irfanengineer

Reputation: 1300

Load your file by sheet index and append your values

$download_file_name = 'DataFile';
 Excel::selectSheetsByIndex(1)->load($file_path, function($reader) use () {
    $reader->sheet('Sheet1', function($sheet) use () {
        //if you want to add value append single value
        $sheet->SetCellValue("A7", "Exporter");
        //if want to append row
        $sheet->appendRow($dispColArray);                  
                     });
}, 'UTF-8')->setFilename($download_file_name)->download('xls');

You can also use for the following function for sheet style

  1. For sheet style

    $sheet->setStyle(array(
        'font' => array(
            'name' => 'Calibri',
            'size' => 9,
            'background' => '#ffffff'
        )
    ));
    
  2. Merge cell and design

    $sheet->mergeCells('A1:D4');
    $sheet->cells('A1:D4', function($cells) {
        $cells->setBackground('#ffffff');
    
    });
    

Upvotes: 1

markantonay
markantonay

Reputation: 142

You can modify the values inside the Excel Load.

$rows = Excel::load($file_path, function($reader)
{
    $reader->sheet('sheet1', function($sheet){
        $sheet->cells('E4', function($cell) {
            $cell->setValue('my value');
        });
    });     

})->store('xls');

Upvotes: 0

Related Questions