Skip to content
Advertisement

Export array to Excelwith MaatwebsiteExcel

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 MaatwebsiteExcelExcel;
use MaatwebsiteExcelConcernsFromCollection;
use MaatwebsiteExcelConcernsExportable;


    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, MaatwebsiteExcelExcel::XLSX, [
                'Content-Type' => 'text/xlsx',
            ]);
        }

Generate data work fine.

My debug return this:

IlluminateSupportCollection {#1621 ▼
  #items: array:10 [▼
    0 => array:22 [▶]
    1 => array:22 [▼
      "pesel" => "13e212"
      "name" => "Lukasz"
      "surname" => "Baranowski"
      "email1" => "name@wp.pl"
      "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.

Advertisement

Answer

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 AppExports;

use MaatwebsiteExcelConcernsFromCollection;

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement