如何设置数字存储为文本laravel excel?

时间:2019-01-20 09:59:57

标签: laravel laravel-excel

这个实际数字

6471050909740010

并在此处将结果替换为excel

6.47105E+15

6471050909740000

这是我的代码

$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);
             }
        });

    });
$collect中的

数据都是字符串事件,数字存储为字符串

2 个答案:

答案 0 :(得分:1)

Larvel maatwebsite/excel版本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']);
        }
    });
});

答案 1 :(得分:0)

我认为您可以执行以下操作

$sheet->setCellValue('H'.$i, $empid);

代替

$sheet->cell('H'.$i, $empid);