Arsen Ibragimov
Arsen Ibragimov

Reputation: 435

How to rename file before ->download() with Laravel Excel

Can't find in docs of Laravel Excel how to give a new name for loaded file before give it for download. I've tried ->setTitle but it doesn't work.

Excel::load(public_path().'/bills/bill.template.xlsx', function($doc) {

            $doc->setTitle = 'test';
            $sheet = $doc->setActiveSheetIndex(0);

            $sheet->setCellValue('G21', '{buyer}');
            $sheet->setCellValue('AB24', '{sum}');
            $sheet->setCellValue('B30', '{sum_propis}');


        })->download('xlsx');

It gives me "bill.template.xlsx" when I'm waiting for "test.xlsx"

Upvotes: 5

Views: 7317

Answers (2)

user10418674
user10418674

Reputation: 1

$data = Excel::download($query, 'TimeSheetExport.xlsx');
$data->setContentDisposition('attachment','TimeSheetExport')->getFile()->move(public_path('xlsx/'), $data->getFile().'xlsx');

$query is our collection.

'TimeSheetExport.xlsx' -> Will not be our file name

in move() you can give your file name in second parameter, and first parameter will be the location for that file

Upvotes: 0

fideloper
fideloper

Reputation: 12293

I haven't used this library before, but looking at the code it looks like you can set the filename attribute which will then get used in the headers to set the name of the file downloaded.

Probably something like:

Excel::load(public_path().'/bills/bill.template.xlsx', function($doc) 
{...})
    ->setFilename('whatever')
    ->download('xlsx');

Upvotes: 8

Related Questions