PhpSpreadsheet: value is interpreted as number even if the cell format is set to text

32 views Asked by At

I first set a cell to be text

$spreadsheet->getActiveSheet()->getStyle( "A1" )->getNumberFormat()
    ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);

Then I set the cell value to a string that may be interpreted as a number

$spreadsheet->getActiveSheet()->setCellValue( "A1", "10.20" );

When I open the Excel file the cell A1 is actually TEXT but the value is

10,2

(my Excel has a preference set to use comma as decimal separator).

So despite the text format and the fact that I also pass a string to setCellValueByColumnAndRow() the trailing zero is lost and dot converted to comma.

I want the cell to be 10.20.

What am I missing?

1

There are 1 answers

0
Paolo On

I figured it out:

I need to use

$spreadsheet->getActiveSheet()->setCellValueExplicit( "A1", $value, PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING );