-1

I'm using PHP , the system should download an excel file once the button clicked. Here's my code - I don't know what's wrong with this since I am using the existing code of the previous IT employee here.

    public function exportToExcel($data) {
        
        
        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
        $reader->setLoadSheetsOnly(["Sheet 1", "CSF"]);
        $spreadsheet = $reader->load("dummy/dummy/assets/template/Client Satisfaction Feedback (CSF) Report.xlsx");
        $sheet = $spreadsheet->getActiveSheet();
        
        
        $entries = array();
        
        $i = 1;
        foreach ($data->indv_feedback as $value) {
            $temp_arr = [
                $i,
                $value->name,
                $value->sex,
                $value->age,
                $value->institution,
                $value->csf_q1,
                $value->csfq_comments1,
                $value->csf_q2,
                $value->csfq_comments2,
                $value->csf_q3,
                $value->csfq_comments3,
                $value->csf_q4,
                $value->csfq_comments4,
                $value->csf_q5,
                $value->csfq_comments5,
                $value->csf_q6,
                $value->csfq_comments6,
                $value->csf_q7,
                $value->csfq_comments7,
                $value->csf_q8,
                $value->csfq_comments8,
                $value->csf_q9,
                $value->csfq_comments9,
                $value->submission_id,
                $value->public_title,
                $value->date_submitted
            ];
            array_push($entries,$temp_arr);
            
            $i++;
        } 
        $sheet->fromArray(
            $entries,   // The data to set
            NULL,        // Array values with this value will not be set
            'A7'         // Top left coordinate of the worksheet range where
                         //    we want to set these values (default is A1)
        );

        $writer = new Xlsx($spreadsheet);
        //  $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        // $writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
        // $writer->setSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $data->title . '.xlsx"');
        header('Cache-Control: max-age=0');
        $writer->save('php://output');

        exit();
    }

Even I tried to used the OPEN AND REPAIR it doesn't work prompting invalid extension or format. Thank you for ur answer guys, in advance. Need to finish this, that's why I post it here. Hope u help guys

qwerty
  • 1
  • 3
  • 1
    Open the downloaded file in a text or hex editor, and check if any PHP error messages might have made their way into it. (Or perhaps HTML content, because you did not properly isolate this from the templating logic, or something like that.) – CBroe Aug 08 '23 at 08:35
  • or var_dump($entries) and validate data – Arkadiusz G. Aug 08 '23 at 10:07

0 Answers0