Laravel-excel: phone number stored as science notation in excel

Created on 12 Sep 2018  路  6Comments  路  Source: Maatwebsite/Laravel-Excel

  • [X] Checked that your issue isn't already filed.
  • [X] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [X] Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.2
  • Laravel version: 5.7
  • Package version: 3

Description

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

Steps to Reproduce

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

Most helpful comment

Try 'B' => '0' instead.

All 6 comments

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
{

Was this page helpful?
0 / 5 - 0 ratings

Related issues

octoxan picture octoxan  路  3Comments

lucatamtam picture lucatamtam  路  3Comments

daraghoshea picture daraghoshea  路  3Comments

pamekar picture pamekar  路  3Comments

alejandri picture alejandri  路  3Comments