Laravel-excel: 0 showing up as blank cell

Created on 29 May 2014  ·  7Comments  ·  Source: Maatwebsite/Laravel-Excel

I apologize if this question has been asked before. I skimmed the closed issues and did not find anything.

When the following example code is ran it has 2 rows but the 0 shows up as a blank cell.

\Excel::create('test', function ($excel) {
    $excel->sheet('sheet', function ($sheet) {
        $sheet->fromArray(array(array('foo' => 0), array('foo' => 4)));
    });
})->export('xls');

I found this post on the PHPExcel site that shows how to get 0's to show up:
https://phpexcel.codeplex.com/discussions/346577

Here is what I had to change my code to so that 0's do not show up as blanks.

\Excel::create('test', function ($excel) {
    $excel->sheet('sheet', function ($sheet) {
        $sheet->fromArray(array(array('foo' => 0), array('foo' => 4)), null, 'A1', true);
    });
})->export('xls');

It also appears that if you pass 0 as a string "0" that it will work too.

I'm surprised that the default behavior is to show 0's as blank cells. Is there an easier way to get 0's to show up? Is there a global config option that I can set in one place so that I don't have to pass all those additional arguments every single time?

Most helpful comment

@philliskiragu I had this same issue, but found this note in the export documentation:

By default 0 is shown as an empty cell. If you want to change this behaviour, you can pass true as 4th parameter:

// Will show 0 as 0
$sheet->fromArray($data, null, 'A1', true);

http://www.maatwebsite.nl/laravel-excel/docs/export

Hopefully this will clear up some confusion if anyone else stumbles across this.

All 7 comments

Will be added with v1.1.0

Hi, I'm having the same problem with imports. Any columns in my CSV that are '0' are returned as null.

Could you make a new issue about this, because it's about importing and not exporting.

I know this issue is closed but I am having the same problem with v2.1 When I export to csv the fields with 0 or null are blank cells.

 Excel::create('issues', function ($excel) use ($issues) {

                $excel->sheet('Sheet 1', function ($sheet) use ($issues) {

                    $sheet->fromArray($issues);
                });
            })->export('csv');

@philliskiragu I had this same issue, but found this note in the export documentation:

By default 0 is shown as an empty cell. If you want to change this behaviour, you can pass true as 4th parameter:

// Will show 0 as 0
$sheet->fromArray($data, null, 'A1', true);

http://www.maatwebsite.nl/laravel-excel/docs/export

Hopefully this will clear up some confusion if anyone else stumbles across this.

@philliskiragu I had this same issue, but found this note in the export documentation:

By default 0 is shown as an empty cell. If you want to change this behaviour, you can pass true as 4th parameter:

// Will show 0 as 0
$sheet->fromArray($data, null, 'A1', true);

http://www.maatwebsite.nl/laravel-excel/docs/export

Hopefully this will clear up some confusion if anyone else stumbles across this.

That link is outdated. For those still looking for zeroes to appear as zeroes, here's the updated link to the part of the document that tells you how
https://docs.laravel-excel.com/3.1/exports/collection.html#strict-null-comparisons
Basically you simply do: use Maatwebsite\Excel\Concerns\WithStrictNullComparison;

Was this page helpful?
0 / 5 - 0 ratings