Considering this method
public function exportExcel(Request $request){
//dd($request);
$spreadsheet = new \App\Reports\FormReport();
$spreadsheet->exportReport($request);
//return response()->json(['message'=>'Success']);
}
where my method exportReport($request)
body is something like this:
$dateTimeNow = Carbon::now();
$spreadsheet->getActiveSheet()
->setCellValue('A1', "This is a test")
->setCellValue('A2', "Hello World")
->setCellValue('A3', Date::PHPToExcel($dateTimeNow));
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Employee List.xlsx"');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit;
and my javascript function:
function postRequest(){
const url = window.location;
const urlReq = url.origin+"/api/export_excel/exportexcel";
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
const list = document.getElementById('lt_employees').value;
if(list){
let obj = {
employees : list,
};
console.log(obj);
const request = new Request(urlReq, {
method: 'POST',
body: JSON.stringify(obj),
headers: new Headers({
'Content-Type': 'application/json',
"X-CSRF-TOKEN": token
})
});
//fetch(request).then(res => res.json()).then(res => console.log(res));
}
}
My problem is upon clicking the button that triggered my javascript method postRequest()
is not generating an excel. What could be the problem here?