How do I export the excel file using phpexcel?

Hi,
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.
error_reporting(E_ALL);

/** 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
$objPHPExcel->getProperties()->setCreator("SDMS")
                             ->setLastModifiedBy("SDMS")
                             ->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->setActiveSheetIndex(0);
$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
$objPHPExcel->getActiveSheet()->getStyle('B1')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);


// Set column widths
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
//$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);

// Set fonts
//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
// Set fills aka backgroun colour
//$objPHPExcel->getActiveSheet()->getStyle('A1:E1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
//$objPHPExcel->getActiveSheet()->getStyle('A1:E1')->getFill()->getStartColor()->setARGB('FF808080');


// Set alignments
//$objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
//$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
//$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

// Set thin black border outline around column
$styleThinBlackBorderOutline = array(
    'borders' => array(
        'outline' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
            'color' => array('argb' => 'FF000000'),
        ),
    ),
);
$objPHPExcel->getActiveSheet()->getStyle('A1:E4')->applyFromArray($styleThinBlackBorderOutline);

// Set page orientation and size
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('$mnemonic');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

/** 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

LimMHAsked:
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.

SwafnilCommented:
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.
<?php
header("Content-Type:application/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

0
SwafnilCommented:
You can even tell the client how the file should be called through the following:
<?php
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:
<?php
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-Type:application/xls");
  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

0

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');
$objWriter->save('php://output');
0
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
PHP

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.