Home > Software engineering >  Export array to Excelwith Maatwebsite\Excel
Export array to Excelwith Maatwebsite\Excel

Time:11-14

I am beginner web developer. I make my project with use Laravel 8 and Maatwebsite Excel.

I make this code to prepare data to save to excel file:

use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;


    public function downloadData1(ReportRequest $request)
        {
            $data = ImmovablesExport::select('*')->orderBy('id', 'DESC')->take(10)->get()->toArray();
            $fileName = 'Raport z ' . now().'.xlsx';
            $sheets = [];
    
            foreach ($data as $i => $item) {
                $sheets[$i]['pesel'] = $this->prepareStringToExport($item['pesel'] ?? '');
                $sheets[$i]['name'] = $this->prepareStringToExport($item['name'] ?? '');
                $sheets[$i]['surname'] = $this->prepareStringToExport($item['surname'] ?? '');
                $sheets[$i]['email1'] = $this->prepareStringToExport($item['email1'] ?? '');
                $sheets[$i]['email2'] = $this->prepareStringToExport($item['email2'] ?? '');
                $sheets[$i]['email3'] = $this->prepareStringToExport($item['email3'] ?? '');
                $sheets[$i]['phone1'] = $this->prepareStringToExport($item['phone1'] ?? '');
                $sheets[$i]['phone2'] = $this->prepareStringToExport($item['phone2'] ?? '');
                $sheets[$i]['phone3'] = $this->prepareStringToExport($item['phone3'] ?? '');
                $sheets[$i]['community'] = $this->prepareStringToExport($item['community'] ?? '');
                $sheets[$i]['city'] = $this->prepareStringToExport($item['city'] ?? '');
                $sheets[$i]['street'] = $this->prepareStringToExport($item['address']['prefix'] ?? '' . ' ' . $item['address']['name'] ?? '');
                $sheets[$i]['building_number'] = $this->prepareStringToExport($item['building_number'] ?? '');
                $sheets[$i]['apartment_number'] = $this->prepareStringToExport($item['apartment_number'] ?? '');
                $sheets[$i]['county'] = $this->prepareStringToExport($item['county'] ?? '');
                $sheets[$i]['district'] = $this->prepareStringToExport($item['district'] ?? '');
                $sheets[$i]['province'] = $this->prepareStringToExport($item['province']['name'] ?? '');
                $sheets[$i]['building_type'] = $this->prepareStringToExport($item['building_type']['name'] ?? '');
    
                $ovens = '';
                if (count($item['ovens']) > 0) {
                    foreach ($item['ovens'] as $ii => $oven) {
                        $ovens .= $oven['name'] . "(moc: " . $oven['power'] . "), źródło: " . $oven['heat_source']['name'] ?? ''
                            . ', funkcje: ' . $oven['oven_function']['name'] ?? '' . ''
                            . ', klasa: ' . $oven['oven_class_number']['name'] ?? '' . ''
                            . ', paliwo: ' . $oven['fuel_type']['name'] ?? ''. '. ';
                    }
                }
                $sheets[$i]['ovens'] = $this->prepareStringToExport($ovens);
    
                $sheets[$i]['granted_comments"'] = $this->prepareStringToExport($item['granted_comments"'] ?? '');
                $sheets[$i]['inspections"'] = $this->prepareStringToExport($item['inspections"'] ?? '');
                $sheets[$i]['oze_installations"'] = $this->prepareStringToExport($item['oze_installations"'] ?? '');
            };
    
            $sheets = collect($sheets); dd($sheets);
            return \Excel::download($sheets, $fileName, \Maatwebsite\Excel\Excel::XLSX, [
                'Content-Type' => 'text/xlsx',
            ]);
        }

Generate data work fine.

My debug return this:

Illuminate\Support\Collection {#1621 ▼
  #items: array:10 [▼
    0 => array:22 [▶]
    1 => array:22 [▼
      "pesel" => "13e212"
      "name" => "Lukasz"
      "surname" => "Baranowski"
      "email1" => "[email protected]"
      "email2" => ""
      "email3" => ""
      "phone1" => "555444333"
      "phone2" => ""
      "phone3" => ""
      "community" => ""
      "city" => "Gdansk"
      "street" => "ul."
      "building_number" => "9"
      "apartment_number" => ""
      "county" => ""
      "district" => ""
      "province" => "2"
      "building_type" => ""
      "ovens" => ""
      "granted_comments"" => "none"
      "inspections"" => ""
      "oze_installations"" => ""
    ]
    2 => array:22 [▶]
    3 => array:22 [▶]
    4 => array:22 [▶]
    5 => array:22 [▶]
    6 => array:22 [▶]
    7 => array:22 [▶]
    8 => array:22 [▶]
    9 => array:22 [▶]
  ]
}

I have problem with save my data to excel file. When I run my code I have downloaded empty file (without my data).

How can I make repair it?

Please help me.

CodePudding user response:

Try to remove your filename and content type :

public function downloadData1(ReportRequest $request)
{
     (...)

     return Excel::download($sheets, 'export.xlsx');
}

EDIT: according to the official documentation, the only way you can export is via FromCollection :

The easiest way to start an export is to create a custom export class. We'll use an invoices export as example.

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;

class InvoicesExport implements FromCollection
{
    public function collection()
    {
        return Invoice::all();
    }
}

In your controller we can now download this export:

Excel::download(new InvoicesExport, 'invoices.xlsx');

If you try to export any other way you will get an empty set.

  • Related