Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

omysurya's avatar

Need Help i got error on Export to Excel

public function toExcelDisc($schedule_id){ $dataArray = [];

    $header = $this->getHeader($schedule_id);
    $discs  = DB::table('interview_test_disc_answer')
        ->where('id_interview_schedules', $schedule_id)
        ->select('sort','ops_a', 'ops_b', 'ops_c', 'ops_d')
        ->orderby('sort', 'ASC')
        ->whereNull('deleted_at');

    $dataArray[] = ['No', 'Col A', 'Col B', 'Col C', 'Col D'];
    $dataArray[] = $discs->get()->toArray();

    //foreach($discs as $row){
    //  $dataArray[] = $row->toArray();
    //}
    //var_dump($dataArray);

    Excel::create('disc-applications', function($excel) use ($dataArray, $header){
        
        $excel->setTitle('Disc Applications');
        $excel->setCreator('Recruit')->setCompany($this->companyName);
        $excel->setDescription('disc-applications file');

        $excel->sheet('sheet1', function ($sheet) use ($dataArray, $header) {
            $sheet->setCellValue('A1', 'Title');
            $sheet->setCellValue('B1', 'Disc Applications');
            $sheet->setCellValue('A2', 'Name');
            $sheet->setCellValue('B2', $header->full_name);
            $sheet->setCellValue('A3', 'Position');
            $sheet->setCellValue('B3', $header->title);
            $sheet->setCellValue('A4', 'Location');
            $sheet->setCellValue('B4', $header->location);

            $sheet->fromArray($dataArray, null, 'A7', false, false);

            $sheet->row(1, function ($row) {

                $row->setFont(array(
                    'bold' => true
                ));

            });

        });
    })->download('xlsx');

}

But doesnt works and get error: "Object of class stdClass could not be converted to string" on vendor\phpoffice\phpexcel\Classes\PHPExcel\Cell\DefaultValueBinder.php:56

0 likes
3 replies
dipakrataniagile's avatar

Had you checked value which was contained in $header variable ?

In your example

$sheet->setCellValue('B2', $header->full_name); $sheet->setCellValue('A3', 'Position'); $sheet->setCellValue('B3', $header->title); $sheet->setCellValue('A4', 'Location'); $sheet->setCellValue('B4', $header->location);

please check that value is fetching in $header->full_name, $header->title & $header->location variable or not?

Please or to participate in this conversation.