How do I export the excel file using phpexcel?

I am new to phpexcel but following the examples I am able to output a .xlsx file.
This was triggered by a click of my Export button. However, the output file is saved at the server end. What I wanted to do was actually to have the xlsx file downloaded to the client pc. (prompt user to save to some location in the client pc) .

Please advse on how this can be doine involving phpexcel.
Thank you.

/** PHPExcel_IOFactory */
//require_once '../Classes/PHPExcel/IOFactory.php';
require_once '../Classes/PHPExcel.php';

require_once '../Classes/PHPExcel/RichText.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties
                             ->setTitle("Mark $prev_yr : $prev_term : $prev_cl : $prev_sid")
                             ->setSubject("Mark $prev_yr : $prev_term : $prev_cl : $prev_sid")
                             ->setDescription("Export Mark $prev_yr : $prev_term : $prev_cl : $prev_sid")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("Export Mark");

// Create a first sheet
$objPHPExcel->getActiveSheet()->setCellValue('A2', $teacheremail);
$objPHPExcel->getActiveSheet()->setCellValue('A1', $prev_sid);
$objPHPExcel->getActiveSheet()->setCellValue('B1', $subjdesc);
$objPHPExcel->getActiveSheet()->setCellValue('F1', $prev_yr);
$objPHPExcel->getActiveSheet()->setCellValue('F2', $prev_term);
$objPHPExcel->getActiveSheet()->setCellValue('D1', $row_start);
$objPHPExcel->getActiveSheet()->setCellValue('D2', $row_end);
$objPHPExcel->getActiveSheet()->setCellValue('E1', $col_start);
$objPHPExcel->getActiveSheet()->setCellValue('E2', $col_end);

// Protect cells
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);	// Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel->getActiveSheet()->protectCells('A1:Z4', 'PHPExcel');

// Unprotect a cell

// Set column widths

// Set fonts
// Set fills aka backgroun colour

// Set alignments

// Set thin black border outline around column
$styleThinBlackBorderOutline = array(
    'borders' => array(
        'outline' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
            'color' => array('argb' => 'FF000000'),

// Set page orientation and size

// Rename sheet

// Set active sheet index to the first sheet, so Excel opens this as the first sheet

/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';

// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

after you call $objWriter->save (...), simply tell the server to send the content as XLS and load the file, which will cause a download prompt on the client.
Please note that the below will only work if there hasn't been any output sent to the client, so you can't use any HTML output before this.
// now you need the path to your saved file, open it and stream it to the client
print file_get_contents("PATHTOYOUR.XLS");

Open in new window

You can even tell the client how the file should be called through the following:
header("Content-Diposition: attachment; filename=download.xls");

and if you are unsure if there has been data sent to the client before, use the following:
if (headers_sent($filename, $linenum)) {
  // in case data has been sent, provide a download link
  print "<a href=\"<?php print $PATHTOYOURXLS;?>\">Download XLS file</a>";
  // for debugging purposes, we will output where the output was first sent to the client
  print "<span style=\"display:none;\">Output started in $filename, $linenum</span>";
} else {
  // otherwise send the file directly to the client
  header("Content-Diposition: attachment; filename=download.xls");
  // now you need the path to your saved file, open it and stream it to the client
  print file_get_contents("PATHTOYOUR.XLS");

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LimMHAuthor Commented:
Thanks for the tips. Apparently using 2007 format had some issues.
I found the solution from the documentation (missed that section earlier.  went straight to the section on writting instead of directing to client browser)
The recommendation was:
// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.