Home Random Page



Define a named range

PHPExcel supports the definition of named ranges. These can be defined using the following code:

// Add some data


$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Firstname:');

$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Lastname:');

$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Maarten');

$objPHPExcel->getActiveSheet()->setCellValue('B2', 'Balliauw');


// Define named ranges

$objPHPExcel->addNamedRange( new PHPExcel_NamedRange('PersonFN', $objPHPExcel->getActiveSheet(), 'B1') );

$objPHPExcel->addNamedRange( new PHPExcel_NamedRange('PersonLN', $objPHPExcel->getActiveSheet(), 'B2') );


Optionally, a fourth parameter can be passed defining the named range local (i.e. only usable on the current worksheet). Named ranges are global by default.

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

  1. Create your PHPExcel spreadsheet
  2. Output HTTP headers for the type of document you wish to output
  3. Use the PHPExcel_Writer_* of your choice, and save to “php://output”


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.


i This may not be safe for unauthorized viewing!
Depending on the configuration of your operating system, temporary storage can be read by anyone using the same temporary storage folder. When confidentiality of your document is needed, it is recommended not to use php://output.

HTTP headers

Example of a script redirecting an Excel 2007 file to the client's browser:



/* Here there will be some code where you create $objPHPExcel */


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




Example of a script redirecting an Excel5 file to the client's browser:



/* Here there will be some code where you create $objPHPExcel */


// redirect output to client browser

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="myfile.xls"');

header('Cache-Control: max-age=0');


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





  • Make sure not to include any echo statements or output any other contents than the Excel file. There should be no whitespace before the opening <?php tag and at most one line break after the closing ?> tag (which can also be omitted to avoid problems).
  • Make sure that your script is saved without a BOM (Byte-order mark). (Because this counts as echoing output)
  • Same things apply to all included files


Failing to follow the above guidelines may result in corrupt Excel files arriving at the client browser, and/or that headers cannot be set by PHP (resulting in warning messages).

Date: 2016-03-03; view: 595

<== previous page | next page ==>
Setting data validation on a cell | Setting the default column width
doclecture.net - lectures - 2014-2021 year. Copyright infringement or personal data (0.002 sec.)