Last active
March 8, 2017 17:23
-
-
Save murilozilli/a4ca6747c06c4078869dd5f21c247cac to your computer and use it in GitHub Desktop.
Zend Framework 2.x(zf2) methods to download excel files using PHPExcel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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