Laravel Excel / PHP Excel: import, modify, upload

I am struggling with Laravel-Excel (Laravel package for PHPExcel).

I cannot change the file. There is one book in my file (called a template). I want to upload a document, modify it, and let the user upload it:

Excel::selectSheets('template') -> load($source, function($file)
{

}) -> download('xls');

      

The code above still works. But I don't understand how to fill the cells. I don't want to directly export the eloquent model, I need to follow a certain structure. The Excel file was not created by me, I just need to fill it with data.

So how can I, for example, fill cell A2 with the first name of the current user?

I am looking for something like

Excel::cell('A2') -> content = $user -> name;

      

This is just an example of what I want to achieve, this is not working code!

Thanks, Luma

+3


source to share


4 answers


Laravel-Excel runs away from PHPExcel and you can run your own PHPExcel methods on objects exposed by Laravel-Excel.

From the docs to get the cell: http://www.maatwebsite.nl/laravel-excel/docs/export#cells

\Excel::create('Document', function($excel) {
    $excel->sheet('Sheet', function($sheet) {
        $sheet->cell('A2', function($cell) {
            $cell->setValue('this is the cell value.');
        });
    });
})->download('xls');

      



And here's a screenshot of the $ cell instance showing what else can be called on it:

Kint dump of the $ cell instance

+3


source


You can use native PHP Excel functions:



Excel::load('file.xls', function($excel)
{
    $excel->getActiveSheet()->setCellValue('A1', 'cell value here');
}) -> download('xls');

      

0


source


I use it like this:

Excel::load('template_orden_compra.xls', function($doc){
   $sheet = $doc->getActiveSheet();
   $sheet->setCellValue('A2', $user->name);
})
->store('xls', storage_path('Excel'));

      

0


source


You can use the following for multiple sheets.

$somevalue = array('abc', 'xyz');

Excel::load('file.xlsx', function($file) use($somevalue) {
   $file->sheet( 'sheetName', function ($sheet) use($somevalue){
        $sheet->setCellValue('A1', $somevalue);
   });
})->export('xlsx');

      

0


source







All Articles