App::import('Vendor','PHPExcel/Classes/PHPExcel'); $objPHPExcel = new PHPExcel(); $objPHPExcel->getActiveSheet()->setTitle('ReceivedMessages'); header('Content-Type: application/vnd.ms-excel'); $file_name = "kpi_form_".date("Y-m-d_H:i:s").".xls"; header("Content-Disposition: attachment; filename=$file_name"); // If you're serving to IE 9,then the following may be needed header('Cache-Control: max-age=1'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5'); $objWriter->save('PHP://output');
当我直接从浏览器调用上面的代码时,会下载结果文件.但是如果我对上面的代码进行ajax调用,我就不会得到下载提示.我可以从控制台选项卡中看到ajax调用已成功完成,并在响应数据中看到一堆随机字符.我假设那是excel对象.
有谁知道如何使用ajax实现下载excel功能?我不想刷新页面.当用户点击“导出”按钮时,应该对PHP文件进行ajax调用并提示用户下载.