Unable to export data into xls file Php

Viewed 28

I am working with Php Codeigniter and i am trying to download "xls",Xls is uploading to server and showing data "correctly" but downloading with different name "downloadreport.xls" with empty data,In other words file is uploading successfully but downloading with wrong name and empty data.

Here is my current code

public function downloadreport()
     {
        $reportName="data";
         $path=APPPATH.'file/'.rand(10,100) . '-' . $reportName . '.xls';
            $id=$_GET['id'];
            $result=$this->Crud_model->downloadreports($id);
            $spreadsheet = new Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();
            $sheet->setCellValue('A1', 'Id');
            $rows = 2;
            foreach ($result as $val){
                $sheet->setCellValue('A' . $rows, "Hello world");
                $rows++;
            } 
            $writer = new Xlsx($spreadsheet);
            $writer->save($path);
            header("Content-Type: application/vnd.ms-excel");
        }   
0 Answers
Related