Redirecting output to a client’s web browser

Topics: Developer Forum, Project Management Forum, User Forum
May 16, 2008 at 8:55 AM
Hello All.

I've noticed that paragraph  4.3.32 of the developer document discusses  redirection to the client's browser.

4.3.32.                   <!--[endif]-->Redirect output to a client’s web browser

Sometimes, one really wants to output a file to a client’s browser, especially when creating spreadsheets on-the-fly. There are some easy steps that can be followed to do this:<o:p></o:p>

  • Create your PHPExcel spreadsheet<o:p></o:p>
  • Output HTTP headers for the type of document you wish to output<o:p></o:p>
  • Use the PHPExcel_Writer_* of your choice, and save to “php://output”<o:p></o:p>

PHPExcel_Writer_Excel2007 uses temporary storage when writing to php://output. By default, temporary files are stored in the script’s working directory. When there is no access, it falls back to the operating system’s temporary files location.



Has anyone got a nice code snippet to demonstrate this?

<o:p></o:p>

May 16, 2008 at 1:14 PM
Hey,
This is wat I am doing:

1.create PHPExcel object--> $objPHPExcel = new PHPExcel();
2. Output the headers:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=File.xls");
header("Content-Transfer-Encoding: binary ");


3. Retrieve data to populate the excel sheet(file will be called File.xls)

4: $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);    //  (I want the output for 2003)
   $objWriter->save('php://output');

May 16, 2008 at 9:23 PM
Thanks for this.

Maybe this code snippet could be an addition to the developer documentation.

What do people think?

Coordinator
May 19, 2008 at 6:14 AM
Will add it to the documentation. Thanks!
Aug 12, 2008 at 8:25 AM
hallo all...

i'm newby but i have nice code for example. I 'm trying  to load xlsx file from TEMPLATE_DIR,
adding some additional data, and redirecting the result to client browser.

    include 'PHPExcel/Reader/Excel2007.php';
    include 'PHPExcel/Writer/Excel2007.php';
    if (!file_exists(TEMPLATE_DIR.$filename.'.xlsx')) {
        exit('Template '.$filename.".xlsx doesn't exist.\n");
    }
   
    $objReader = new PHPExcel_Reader_Excel2007();
    $objPHPExcel = $objReader->load(TEMPLATE_DIR.$filename.'.xlsx');
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=$filename.xls");
    header("Content-Transfer-Encoding: binary ");
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $objWriter->setOffice2003Compatibility(true);
   
    //Add some additional data

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

Thanks PHPExcel Team. You have developed magnificent php library.
Aug 12, 2008 at 8:25 AM
Edited Aug 12, 2008 at 8:38 AM
hallo all...

i'm newby but i have nice code for example. I 'm trying  to load xlsx file from TEMPLATE_DIR,
adding some additional data, and redirecting the result to client browser.

    include 'PHPExcel/Reader/Excel2007.php';
    include 'PHPExcel/Writer/Excel2007.php';
    if (!file_exists(TEMPLATE_DIR.$filename.'.xlsx')) {
        exit('Template '.$filename.".xlsx doesn't exist.\n");
    }
   
    $objReader = new PHPExcel_Reader_Excel2007();
    $objPHPExcel = $objReader->load(TEMPLATE_DIR.$filename.'.xlsx');

   //Add some additional data here :)

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=$filename.xls");
    header("Content-Transfer-Encoding: binary ");
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $objWriter->setOffice2003Compatibility(true);
    $objWriter->save('php://output');

Thanks PHPExcel Team. You have developed magnificent php library.