PHPExcel:下载文件

时间:2018-02-05 10:58:30

标签: php excel phpexcel

我的PHPExcel功能有问题。 我想生成一个简单的excel文件进​​行测试。这在我看来很好(我在另一个网站上找到了):

<?php 
$objPHPExcel = new Actimage_PhpExcel();

// Create new PHPExcel object

// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                             ->setLastModifiedBy("Maarten Balliauw")
                             ->setTitle("Office 2007 XLSX Test Document")
                             ->setSubject("Office 2007 XLSX Test Document")
                             ->setDescription("Test document for Office 2007     XLSX, generated using PHP classes.")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B2', 'world!')
            ->setCellValue('C1', 'Hello')
            ->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A4', 'Miscellaneous glyphs')
            ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the     first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-    officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_clean();
$objWriter->save('php://output');
exit;
    ?>

它会下载正确的excel文件。但是,当我把它放入我的控制器(sendemaildemande动作)时,它无法下载文件,响应调试会向我显示一些不可读的字符。

$('#listedemandes tbody').on('click', '.valider-demande', function () {
    $.ajax({
            url: '<?php echo $this->url(array('controller' => 'demandes', 'action' => 'sendemaildemande'),null,true);?>',
            type: 'get',
            datatype: 'json',
            success: function(data) { alert("ok");}
    });

我错过了什么吗?

由于

0 个答案:

没有答案