PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.32k stars 3.45k forks source link

Keep a decimal number stay original from a PHP array, even in 0 number ike .0000 #4195

Open ahmadfadlydziljalal opened 7 hours ago

ahmadfadlydziljalal commented 7 hours ago

This is:

- [1 ] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Keep a decimal number stay original from a PHP array,

As you can see at the image below: Real value: 149819.78, Formatted value: 149819.7800

What is the current behavior?

Format is OK, but not the actual value. image

What are the steps to reproduce?

Please consider that you have an array like this:

$array = [
    0 => [
        'bruto' => '149819.7800'
        'volume' => '153.0000'
    ]
    1 => [
        'bruto' => '10594.0000'
        'volume' => '15.5650'
    ]
    2 => [
        'bruto' => '15750.0000'
        'volume' => '25.0000'
    ]
    3 => [
        'bruto' => '17263.0800'
        'volume' => '63.4618'
    ]
    4 => [
        'bruto' => '66600.0000'
        'volume' => '75.0000'
    ]
    5 => [
        'bruto' => '16128.0000'
        'volume' => '30.0000'
    ]
    6 => [
        'bruto' => '14280.0000'
        'volume' => '30.0000'
    ]
    7 => [
        'bruto' => '4806.4000'
        'volume' => '91.1700'
    ]
    8 => [
        'bruto' => '20732.0000'
        'volume' => '25.0000'
    ]
    9 => [
        'bruto' => '13004.2300'
        'volume' => '44.6290'
    ]
    10 => [
        'bruto' => '13929.0000'
        'volume' => '21.0080'
    ]
    11 => [
        'bruto' => '13280.0000'
        'volume' => '25.0000'
    ]
    12 => [
        'bruto' => '11929.0000'
        'volume' => '15.3400'
    ]
    13 => [
        'bruto' => '2884.9000'
        'volume' => '16.6950'
    ]
]
``

Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of code that exhibits the issue without relying on an external Excel file or a web server:

```php
<?php

        $spreadsheet = IOFactory::createReader('Xls')->load(
            Yii::$app->basePath . '/modules/utilities/views/flat-file-import/ssm/_header_bea_cukai_format.xls'
        );
        $activeSheet = $spreadsheet->setActiveSheetIndexByName('Detil');
        $activeSheet->fromArray($this->getTabDetil(), '', 'A2'); // $this->getTabDetil() is an array as a mention before
        $highestRow = $activeSheet->getHighestRow();
        for ($i = 2; $i <= $highestRow; $i++) {
            $activeSheet->getStyle('A' . $i)->getNumberFormat()->setFormatCode('0.0000');
            $activeSheet->getStyle('B' . $i)->getNumberFormat()->setFormatCode('0.0000');
        }

       # Sending headers to force the user to download the file
        $objWriter = IOFactory::createWriter($spreadsheet, 'Xls');

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . time() . $this->flatFileImport->id . '.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
        exit;

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

Which versions of PhpSpreadsheet and PHP are affected?

oleibman commented 7 hours ago

A number on the formula bar will display as Excel wishes to display it; this will be without trailing zeros in the decimal portion. You can see the same behavior in Excel quite easily - if you enter 1.00 in a cell (with default formatting), Excel will display it as 1 in both the cell and the formula bar. What kind of problem is this causing for you? In particular, why do you feel that the value it is displaying is "wrong"?

If, as it seems, you want the cell contents to be treated as a string, you might want to try StringValueBinder.