虽然使用正确的headertypes导出到XLS输出仍然是垃圾

时间:2016-10-17 09:17:08

标签: phpexcel

我已尝试过使用PHP导出到Excel工作表的其他问题中提供的一些解决方案,仍然无法正常工作。另外我提到PHPExcel库示例仍然卡住了。

这是我的代码。

<?php
    ob_start();
    require_once 'Classes/PHPExcel.php';
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()   ->setCreator("MeritTrac")
                                    ->setTitle("PHPExcel Test document")
                                    ->setSubject("PHPExcel Subject")
                                    ->setDescription("This is generatred by PHPExcel");

    $objPHPExcel    ->setActiveSheetIndex(0)
                    ->setCellValue('A1','Hello')
                    ->setCellValue('B1','Test')
                    ->setCellValue('C1','Color')
                    ->setCellValue('D1','World');

    $objPHPExcel    ->setActiveSheetIndex(0)
                    ->setCellValue('A4','Miscellaneous Glyph')
                    ->setCellValue('A5','ífá');

    $objPHPExcel    ->setActiveSheetIndex(0);


    header('Content type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    //header('Content type:application/vnd.ms-excel');
    header('Content disposition: csv;filename="download.xlsx"');
    header('Cache-Control: max-age=0');
    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');
    echo "Test print";

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;
?>
<?php
ob_end_flush();
?>

输出: http://imgur.com/a/5AsWF 请找到我所遗漏的内容,因为这段代码给了我垃圾输出。

0 个答案:

没有答案