0

I have written code for exporting the data(which is from Query builder) and downloading that sheet immediately to user's browser. I am using Laravel-Excel package for this, but the csv file is not getting downloaded.

I want the csv file to be 'Tab' delimited like below:

 Supplier     Campaign    Disposition
sup-value1   cam-value1    dis-value1
sup-value2   cam-value2    dis-value2
sup-value3   cam-value3    dis-value3

My input as array is :

$results = 
Array
(
[0] => Array
    (
        [Supplier] => Avikmithran
        [Campaign] => krisha
        [Disposition] => testing purpose
    )
[1] => Array
    (
        [Supplier] => Avikmithran
        [Campaign] => testscaseqwe
        [Disposition] => this is the disposition
    )
[2] => Array
    (
        [Supplier] => Avikmitra
        [Campaign] => KrishnaL
        [Disposition] => this is testing
    )
[3] => Array
    (
        [Supplier] => Sonali
        [Campaign] => starslab
        [Disposition] => this is the disposiiton
    )
)

CSV file download code is:

Excel::create('Statistics', function($excel) use($results) {

            $excel->sheet('Statistics', function($sheet) use($results) {

                $sheet->with($results);

            });

})->export('csv');

It is driving the nuts, I have wasted 2 days on this already. Can anyone please tell me what is my mistake? Thanks.

Prasad Patel
  • 707
  • 3
  • 16
  • 53

1 Answers1

0

You need to use the fromArray method and also pass $results by reference:

Excel::create('Statistics', function($excel) use(&$results) 
{
    $excel->sheet('Statistics', function($sheet) use(&$results) 
    {
        $sheet->fromArray($results);
    });

})->export('csv');
Mozammil
  • 8,520
  • 15
  • 29
  • Thanks for the reply. I have already used 'fromArray' method but I didn't pass $results by reference. I will try to pass $results by reference and will let you know. – Prasad Patel Jun 05 '17 at 01:52
  • I have tried your solution but its not working. Response is showing if I inspect browser>Network Tab, but the csv file is not getting downloaded. – Prasad Patel Jun 05 '17 at 04:35
  • Do I need to add/change any parameters to the file located at "app/config/packages/maatwebsite/excel/config.php" or any other file. – Prasad Patel Jun 05 '17 at 04:41