Laravel-excel: Number stored as Text

Created on 20 Oct 2014  路  5Comments  路  Source: Maatwebsite/Laravel-Excel

So if I create a sheet from an array:

$sheet->fromArray($rows);

and I apply a number filter:

$sheet->setColumnFormat([
                    'D' => '0',
                ]);

The numbers as still stored as 'text' in Excel - and I cannot do any calculations on them.

I know the column format is working - because I can see in Excel the column is actually formatted as a 'number' - but it says the individual cells in the D column as being stored as text.

Any ideas how to fix this?

Most helpful comment

Try like this.
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '1234', PHPExcel_Cell_DataType::TYPE_STRING);

All 5 comments

Apparently this doesn't work like I had expected.
Behind the scenes we are using the PHPExcel methods:

$this->getStyle($column)
    ->getNumberFormat()
    ->setFormatCode($format);

->getStyle() should work for columns as well, as far as I can see in the source code. But this doesn't seem to be the case for column formatting.

It works fine when I do something like D1:D10 (range). You could do something like:

$highestRow = count($rows);
$sheet->setColumnFormat([
    'D1:D' . $highestRow => '0',
]);

Neither Column nor Row styles are supported by PHPExcel, only cells and cell ranges

I worked it out. My model had accessors that changed the values, and they were bring interpreted as strings. Typecasting them to int fixed the issue.

Excuse me @TheShiftExchange can you tell me how you get row number of excell sheet ? I have some specific validation per row , so if my error is on row 2 i should return number 2 ?

Can you tell me how to do that ?

Thanks Maja

Try like this.
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '1234', PHPExcel_Cell_DataType::TYPE_STRING);

Was this page helpful?
0 / 5 - 0 ratings