Search code examples
phpbrowserphpexcel

Displaying excel file in a browser. PHPExcel


For example I'm using such a code:

<?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');
?>

It downloads report.xlsx file and doesn't display it in a browser. How do I make it?

Thanks!


Solution

  • Remove this line...

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

    The user must have something in their browser capable of viewing the Excel file too.