在浏览器中显示Excel文件。 PHPExcel

时间:2012-01-18 06:42:45

标签: php browser phpexcel

例如我正在使用这样的代码:

<?php

    require_once("D:\server/www/cls/PHPExcel.php");
    require_once("D:\server/www/cls/PHPExcel/IOFactory.php");

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', 'HeaderB');    
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C2', 'HeaderC');
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D2', 'HeaderD');    
    ob_end_clean();

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="report.xlsx"');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();

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

它会下载report.xlsx个文件,但不会在浏览器中显示。我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:3)

删除此行...

header('Content-Disposition: attachment;filename="report.xlsx"');

用户必须在浏览器中拥有能够查看Excel文件的内容。