问题描述
我目前在使用AJAX调用和Maatwebsite(3.1版)导出excel时遇到一些问题。 我想要的是从网格中获取过滤器数据,然后进行经典导出。
首先,这是我单击“导出”按钮时触发的视图中的AJAX调用:
$.ajax({
type:"post",url: btnExportExcel.attr('data-url-export'),dataType: "json",responseType:'blob',data: {
projetsJSON: JSON.stringify(projets)
},success:function() {
alert('success')
}
编辑:
实际上,ajax调用返回错误:SyntaxError: Unexpected end of JSON input
。
但是我仍然不知道问题出在哪里,我的json结构似乎还可以:
[{"libelle":"quam","budget_ht":98829,"chef_proj":"toto toto","dt_creation":"2020-02-26","dt_fin":"2020-03-26","famille":"Culturel","obj_collect":"26%","ouverture":"Particuliers et entreprises","type_proj":"Récurrent annuel"},{"libelle":"quia","budget_ht":92845,"dt_fin":"2020-06-26","famille":"Sportif","obj_collect":"57%","budget_ht":77938,"famille":"Social","obj_collect":"58%","type_proj":"Récurrent annuel"}]
EDIT2:
我从AJAX调用中删除了dataType
属性,现在ajax调用可以正常工作了,但是我得到了这样的xls编码文件:
PKWbSQGD²Xð[Content_Types].xmlMNÃ0÷"ò%nY vAa (0ö¤±êØgúw{&i@ÕnbEö{ßøyìÑdÛ¸l
mð¥×ÁX¿(ÅÛü)¿òF¹à¡;@1_æ»±Øc)j¢x/%êEày¦
©QÄ¿i!£ÒKµy3ÜJ<§Z1½0?YÙL%zV
因此AJAX调用工作正常(状态= 200 OK),并在我的控制器中调用此函数:
public function exportExcel(Request $request) {
// datas are in JSON format so i decode it to create an array
$projets = json_decode($request->projetsJSON);
// and i call the ProjetExport class to pass the datas that i want to export
Excel::download(new ProjetsExport($projets),'projets'. date('Y-m-d') . '.xlsx');
}
我从这里逐步跟踪文档:https://docs.laravel-excel.com/3.1/exports/collection.html#using-arrays。
这是ProjetExport类:
<?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
{
return $this->request;
}
/**
* 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
];
}
}
当我从ProjetExport类将数据转储到 array 函数中时:
<?PHP
/**
* @return array
*/
public function array():array
{
dd($this->request);
return $this->request;
}
我得到了要导出的数据数组:
array:125 [
0 => {#416
+"libelle": "illo"
+"budget_ht": 70979
+"chef_proj": "Toto toto"
+"dt_creation": "2020-02-26"
+"dt_fin": "2020-07-26"
+"famille": "Cultuel"
+"obj_collect": "34%"
+"ouverture": "Entreprises uniquement"
+"type_proj": "Pluriannuel"
}
1 => {#411
+"libelle": "repellendus"
+"budget_ht": 106992
+"chef_proj": "Toto toto"
+"dt_creation": "2020-02-26"
+"dt_fin": "2020-03-26"
+"famille": "Patrimoine"
+"obj_collect": "22%"
+"ouverture": "Entreprises uniquement"
+"type_proj": "Unique"
}
没有下载任何内容。 我没有任何错误消息或任何可向我解释我做错了什么的消息,因此,如果有人可以提供帮助,那将是很好的。 谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)