通过 PHPExcel 和 Codeigniter 以及 Ajax 导出到 Excel

export to Excel by PHPExcel and Codeigniter and Ajax

我想通过 PHPExcel 导出 xls 并使用 Codeigniter 和 AJAX,但我没有得到任何文件,请帮助我

HTML 按钮代码:

<button class="buttonExcel" id="ButtonExcel" onclick="getExcel()" ></button>

我的 AJAX 代码 运行 很好,但不要为我创建任何文件:

<script>
function getExcel()
    {
        $.ajax({
                       url:'<?=base_url();?>tops/posts/create_result',
                       destroy: true,
                       type: 'POST',
                       data: '',
                       beforeSend: function() {
                          $("#ButtonExcel").removeClass("buttonExcel");
                          $("#ButtonExcel").addClass("buttonExcel-getExcel");
                          $("#ButtonExcel").blur();
                       },
                       success: function(response){


                               window.open('<?=base_url();?>tops/posts/create_result','_blank');
                    },
                        error: function(){
                            alert("error when get data");

                        }
                });
        }

</script>

我的控制器 运行 没有 ajax:

 public function create_result()
   {

         set_time_limit(600);
         $this->load->model('tops/Posts_model');
         $list = $this->Posts_model->create_result(); 


         require(APPPATH."third_party/PHPExcel-1.8/Classes/PHPExcel.php");
         require(APPPATH."third_party/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel5.php");

         $objPHPExcel = new PHPExcel();

         $objPHPExcel->getProperties()->setCreator("");
         $objPHPExcel->getProperties()->setLastModifiedBy("");
         $objPHPExcel->getProperties()->setTitle("");
         $objPHPExcel->getProperties()->setSubject("");
         $objPHPExcel->getProperties()->setDescription("");

         $objPHPExcel->setActiveSheetIndex(0);

         $sheet = $objPHPExcel->getActiveSheet();

         $sheet->setCellValue("A1","text");
         $sheet->setCellValue("B1","time");
         $sheet->setCellValue("C1","date");
         $sheet->setCellValue("D1","type");
         $sheet->setCellValue("E1","view");

         $row = 2;

         foreach ($list as $key => $value)
         {

             $sheet->setCellValue("A".$row,$value['MsgText']);
             $sheet->setCellValue("B".$row,$value['MsgTime']);
             $sheet->setCellValue("C".$row,$value['MsgDate']);
             $sheet->setCellValue("D".$row,$value['MsgType']);
             $sheet->setCellValue("E".$row,$value['CountView']);
             $row++;
             }

         $filename = "Task-Exportet-on-".date("Y-m-d-H-i-s").".xls";
         $sheet->setTitle("Task-Overview");

         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //mime type
        header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
        header('Cache-Control: max-age=0');


        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
                //force user to download the Excel file without writing it to server's HD
        $objWriter->save('php://output');
        set_time_limit(30);
        exit;
    }

代码似乎工作正常,您无法获取文件的唯一原因是允许在浏览器中弹出 window。

我通过在我的控制器中将此 headers 替换为较旧的 headers 解决了这个问题:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");