使用AJAX调用PHPExcel文件下载

问题描述 投票:0回答:1

我有一个用于导出文件的 PHPExcel 代码,当我通过正常的静态输入运行它时,它工作得很好,但是当尝试使用 ajax 调用时,文件没有被下载。 生成代码的php代码如下

include "dbconnect.php";
include 'PHPExcel/Classes/PHPExcel/IOFactory.php';
include 'PHPExcel/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
if (isset($_POST['sql'])) {
    $sql=mysql_query($_POST['sql']);
    echo $sql;
    if($sql === FALSE) {
        die(mysql_error()); // TODO: better error handling
    }
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setCellValue('A1', 'A');
    $objPHPExcel->getActiveSheet()->setCellValue('B1', 'B');
    $objPHPExcel->getActiveSheet()->setCellValue('C1', 'C');
    $objPHPExcel->getActiveSheet()->setCellValue('D1', 'D');
    $objPHPExcel->getActiveSheet()->setCellValue('E1', 'E');
    $objPHPExcel->getActiveSheet()->setCellValue('F1', 'F');
    $objPHPExcel->getActiveSheet()->setCellValue('G1', 'G');
    $objPHPExcel->getActiveSheet()->setCellValue('H1', 'H');
    $objPHPExcel->getActiveSheet()->setCellValue('I1', 'I');
    $objPHPExcel->getActiveSheet()->setCellValue('J1', 'J');
    $objPHPExcel->getActiveSheet()->setCellValue('K1', 'K');
    $objPHPExcel->getActiveSheet()->setCellValue('L1', 'L');
    $objPHPExcel->getActiveSheet()->setCellValue('M1', 'M');

    $n=2;

    while($sqlr= mysql_fetch_array($sql)) {
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$n, $sqlr['a']);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$n, $sqlr['b']);
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$n, $sqlr['c']);
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$n, $sqlr['d']);
        $objPHPExcel->getActiveSheet()->setCellValue('E'.$n, $sqlr['e']);
        $objPHPExcel->getActiveSheet()->setCellValue('F'.$n, $sqlr['f']);
        $objPHPExcel->getActiveSheet()->setCellValue('G'.$n, $sqlr['g']);
        $objPHPExcel->getActiveSheet()->setCellValue('H'.$n, $sqlr['h']);
        $objPHPExcel->getActiveSheet()->setCellValue('I'.$n, $sqlr['i']);
        $objPHPExcel->getActiveSheet()->setCellValue('J'.$n, $sqlr['j']);
        $objPHPExcel->getActiveSheet()->setCellValue('K'.$n, $sqlr['k']);
        $objPHPExcel->getActiveSheet()->setCellValue('L'.$n, $sqlr['l']);
        $objPHPExcel->getActiveSheet()->setCellValue('M'.$n, $sqlr['m']);
        $n++;
    }
    $objPHPExcel->getActiveSheet()->setTitle('SORTED');

    $objPHPExcel->setActiveSheetIndex(0);
    for($col = 'A'; $col !== 'Z'; $col++) {
        $objPHPExcel->getActiveSheet()
            ->getColumnDimension($col)
            ->setAutoSize(true);

        $objPHPExcel->getActiveSheet()->getStyle("A1:M1")->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()
            ->getStyle('A1:M1')
            ->getFill()
            ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
            ->getStartColor()
            ->setARGB('75b847');
    }

    $objPHPExcel->setActiveSheetIndex(0);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="reports.xls"');
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');
}
else {
    echo "no luck";
}
?>

JS如下

$(document).on("click", "#submit2", function() {
    var sql=$("#sql").val();
    console.log(sql);
    $.ajax({
        type: "POST",
        url: 'exreports.php',
        data: {
            sql : sql
        },
        success: function () {
            window.open(this.url,'_blank' );
        }
    });

});

但这不是下载文件,我不想刷新页面,因为用户可能会执行一些其他操作,如果有任何方法可以在同一窗口中下载那就太好了。

提前致谢

php jquery ajax download phpexcel
1个回答
0
投票

https://stackoverflow.com/a/27702111/8221063

在ajax成功函数中添加

target=_blank
,如下所示

success: function(){
  window.open('http://YOUR_URL','_blank' );
},

否则您可以巧妙地使用 jQuery 触发功能等在新选项卡中打开 Excel 下载链接。

© www.soinside.com 2019 - 2024. All rights reserved.