AJAX调用PHPExcel输出文件为空

时间:2017-09-06 12:13:33

标签: php ajax

我从这段代码开始:

的index.html:

$("button").click(function(){
    $.ajax({
    type: "POST",
    url: "htmltable_to_excel/savexls.php",
    cache: false,
    data: JSON.stringify({'name': 'test'}),
    dataType: 'text',
    contentType: 'text/plain',
    async: false,
    success: function(result)
    {
        alert(result);
        //window.open("htmltable_to_excel/savexls.php",'_blank');
    }
});

savexls.php:

$val = file_get_contents('php://input');
$json = json_decode($val, true);
echo $json['name'];

返回'测试'。 AJAX调用似乎很好。

我有通过PHPExcel创建xlsx的代码:

savexls.php:

require_once 'PHPExcel.php';

$val = file_get_contents('php://input');
$json = json_decode($val, true);

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', $json['name']);

// Redirect output to a client’s web browser (Excel2007)
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

ob_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
// header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter->save('php://output');

但是当我打开创建的xlsx文件时,单元格为空。我忘记了什么?可能是什么问题呢?有没有人有想法?谢谢!

0 个答案:

没有答案