Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while exporting Excel using PHP Spreadsheet and CodeIgniter 3 in PHP 8.2

I’m facing an issue while exporting Excel using PHP Spreadsheet and CodeIgniter 3 in PHP 8.2. The error message is shown in the following image:

enter image description here

Here is the code snippet that I’m using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$fileName = 'activityReport.xlsx';
                        $this->load->library('ciSpreadsheet');
                        $spreadsheet = new Spreadsheet();
                        $spreadsheet->getProperties()->setCreator("CalibOn")
                                ->setLastModifiedBy("CalibOn")
                                ->setTitle("Activity Report")
                                ->setSubject("Activity Report")
                                ->setDescription("Activity Report")
                                ->setKeywords("")
                                ->setCategory("Report");
                        $spreadsheet->setActiveSheetIndex(0);
                        $col_array = array('Activity Date', 'Activity Time', 'Account Name', 'Activity Type', 'Contacted', 'Outcome', 'Description', 'Activity By', 'Created Date', 'Created Time');
                        $col_array_key = array('ActivityDate', 'ActivityTime', 'AccountName', 'ActivityType', 'Contacted', 'Outcome', 'Description', 'ActivityBy', 'CreatedDate', 'CreatedTime');
                        $col_count = 1;
                        $row_count = 1;
                        $sheet = $spreadsheet->getActiveSheet();
                        foreach ($col_array as $value) {
                            $sheet->setCellValue($this->getNameFromNumber($col_count) . $row_count, $value);
                            $col_count++;
                        }
                        $row_count++;

                        foreach ($report_array as $key => $value) {

                            $col_count = 1;
                            foreach ($col_array_key as $key_c => $value_col) {

                                $sheet->setCellValue($this->getNameFromNumber($col_count) . $row_count, $value->$value_col);
                                $col_count++;
                            }
                            $row_count++;
                        }

                        header('Content-Type: application/vnd.ms-excel');
                        header('Content-Disposition: attachment;filename="' . $fileName . '"');
                        header('Cache-Control: max-age=0');
                        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');


                        $writer->save("php://output");
                        exit;

The reportData variable, which is fetched from the database, follows the sample format below:

Array(
    [0] => Array(
        [ActivityDate] => 2022-06-28,
        [ActivityTime] => 14:00:00,
        [AccountName] => "Name",
        [ActivityType] => "Visit",
        [Description] => "desc",
        [Contacted] => "completed by",
        [Outcome] => ,
        [ActivityBy] => "generated by",
        [CreatedDate] => 2022-07-08,
        [CreatedTime] => 17:13:59
    )
);

I’m seeking assistance to resolve the error and successfully export the Excel file. Any guidance or suggestions on fixing the issue would be highly appreciated.

>Solution :

Please use this after writer initialization.

$writer->setOffice2003Compatibility(true);

Also,Change the header content type to the below given snippet

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading