Jul 2, 2020
0
Level 1
Maatwebsite Excel/Laravel - Downloading excel not working with AJAX call
Hello,
I'm developing web application with Laravel and i have some issue with the Maatwebsite export excel with an AJAX call : i don't understand why the file is not upload because i don't have any error message and the AJAX call seems to work fine.
Here is the AJAX call in the view :
$.ajax({
type:"post",
url: btnExportExcel.attr('data-url-export'),
dataType: "json",
responseType:'blob',
data: {
projetsJSON: JSON.stringify(projets)
},
success:function() {
console.log("success");
}
})
The controller :
/**
* Fonction d'export Excel
* @return Excel
*/
public function exportExcel(Request $request) {
return Excel::download(new ProjetsExport($request),'projets'. date('Y-m-d') . '.xlsx');
}
And the "ProjetExport" class :
<?php
namespace App\Exports;
use App\Models\Projet;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class ProjetsExport implements FromArray, WithHeadings, WithMapping
{
protected $request;
public function __construct($request)
{
$this->request = $request;
}
/**
* @return array
*/
public function array():array
{
$projets = json_decode($this->request->projetsJSON);
return $projets;
}
/**
* Initialisation des headers des colonnes du fichier Excel.
*/
public function headings(): array
{
return ['Libellé','Date de création','Date de fin','Budget HT','Objectif','Etat','Type','Ouverture','Chef de projet','Famille'];
}
/**
* Formattage des données.
*/
public function map($row): array
{
return [
$row->libelle,
(new \DateTime($row->dt_creation))->format('d/m/Y'),
(new \DateTime($row->dt_fin))->format('d/m/Y'),
$row->budget_ht,
$row->obj_collect,
$row->type_proj,
$row->ouverture,
$row->chef_proj,
$row->famille
];
}
}
If someone have an idea or some clue to help me, because i'm a bit stuck right now. Thanks in advance !
Please or to participate in this conversation.