Search code examples
laravellaravel-excel

How to set Number stored as text laravel excel?


this actual number

6471050909740010

and get replace by excel here the result

6.47105E+15

6471050909740000

This my code

$collect = collect($model->toArray());

$excelData = Excel::create('Endorsement', function($excel) use ($collect) {

             $excel->sheet('Endorsement', function ($sheet) use ($collect) {

             $this->template($sheet);

             foreach ($collect as $key => $rest) {
                $empid = strval($rest['empid']);

                $i = $key+2;
                $sheet->cell('A'.$i, $rest['ttype']);
                $sheet->cell('H'.$i, $empid);
             }
        });

    });

data inside $collect all is string event the number store as string


Solution

  • Larvel maatwebsite/excel version 2.*

    Excel::create('Endorsement', function ($excel) use ($collect) {
    
        $excel->sheet('Endorsement', function ($sheet) use ($collect) {
    
            $sheet->setColumnFormat(array(
                'H' => \PHPExcel_Style_NumberFormat::FORMAT_TEXT,
                'AS' => \PHPExcel_Style_NumberFormat::FORMAT_TEXT,
            ));
    
            $this->template($sheet);
    
            foreach ($collect as $key => $rest) {
    
                $i = $key + 2;
    
                $sheet->setCellValueExplicit('H' . $i, $rest['empid']);
                $sheet->setCellValueExplicit('AS' . $i, $rest['account_no']);
            }
        });
    });