Skip to content

Instantly share code, notes, and snippets.

@murilozilli
Last active March 8, 2017 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murilozilli/a4ca6747c06c4078869dd5f21c247cac to your computer and use it in GitHub Desktop.
Save murilozilli/a4ca6747c06c4078869dd5f21c247cac to your computer and use it in GitHub Desktop.
Zend Framework 2.x(zf2) methods to download excel files using PHPExcel
<?php
namespace App\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class IndexController extends AbstractActionController {
public function indexAction() {
$objWriter = \PHPExcel_IOFactory::createWriter($modelService->getExcel()/*returns a PHPExcel object*/, 'Excel2007');
ob_start();
$objWriter->save('php://output');
$excelOutput = ob_get_clean();
return $this->returnExcel(["status" => 200, 'excelOutput' => $excelOutput], 'otherFileName');
}
protected function returnExcel(array $data, $filename = 'data') {
$this->response->getHeaders()->addHeaders(array(
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition' => 'attachment;filename="'. $filename .'xlsx"',
'Cache-Control' => 'max-age=0',
));
$this->response->setStatusCode($data['status']);
$this->response->setContent($data['excelOutput']);
return $this->response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment