4

I'm struggeling with Laravel-Excel (Laravel Package of PHPExcel).

I'm not able to modify a file. My file has one workbook (called template). I want to load the document, modify it and let the user download it:

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

}) -> download('xls');

Code above works so far. But I don't get how to fill cells. I don't want to export an eloquent model directly, 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 firstname of the current user?

I search for something like

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

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

Thanks, LuMa

4 Answers 4

3

Laravel-Excel runs off of PHPExcel, and you can run native PHPExcel methods on the 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 is a screenshot of the $cell instance showing what else can be called on it:

Kint dump of the $cell instance

Sign up to request clarification or add additional context in comments.

1 Comment

@Laravelian Hey bro. scnshot gives nice functionality, What is this ? any app or library
2

I use that in this way:

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

Comments

0

You can use native PHP Excel functions:

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

Comments

0

You can use 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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.