Search code examples
phprestyii2phpexcel

Yii2 download model as an excel file


I'm working on a Yii2 rest Api that's connected to AngularJS front end.

I'm trying to build an actionDownloadAsExcel method that can help me download a specific model. What would be a good way to do this?

I installed "phpOffice\phpExcel" but I don't really know how to use it for my purpose.


Solution

  • You can find all the data you want to save to the excel, print that using an html table and setting a proper excel header before the view is rendered:

    header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
    header("Content-Disposition: attachment; filename=abc.xls");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    

    Source: PHP Excel Header