I want to store phone number to my database then export it to excel , the issue here is my phone number convert to science notation
i have tried this but it didnt work
public function columnFormats(): array
{
return [
'B' => "@",
];
Expected behavior:
5345700755 in excel
Actual behavior:
5.35E+09 instead of 5345700755
Try 'B' => '0' instead.
same issue @patrickbrouwers 馃槥
this is my whole class
<?php
namespace App\Exports;
use App\data;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class DataExport implements FromCollection,WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return data::all();
}
public function headings(): array
{
return [
'#',
'phone',
'name',
];
}
public function columnFormats(): array
{
return [
'B' => "0",
];
}
}
Most likely due to float imprecision then https://github.com/PHPOffice/PhpSpreadsheet/issues/168
You could implement your own value binder and set it via \PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder($yourvaluebinder);
my last solution was writing this in my model
protected $append = ['phone'];
public function getPhoneAttribute(){
if($this->attributes['phone'][0]!="0"){
return "0".$this->attributes['phone'];
}
return $this->attributes['phone'];
}
That's an option as well ;)
same issue @patrickbrouwers 馃槥
this is my whole class
<?php namespace App\Exports; use App\data; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithHeadings; class DataExport implements FromCollection,WithHeadings { /** * @return \Illuminate\Support\Collection */ public function collection() { return data::all(); } public function headings(): array { return [ '#', 'phone', 'name', ]; } public function columnFormats(): array { return [ 'B' => "0", ]; } }
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
class SinglesExportDemo implements FromCollection, WithMapping, WithHeadings, WithColumnFormatting
{
Most helpful comment
Try
'B' => '0'instead.