skip to Main Content

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:

$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.

2

Answers


  1. Im not sure if this is the only issue, however your header is for xls and the file is xlsx.
    try:

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    
    Login or Signup to reply.
  2. 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');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search