Forum


Replies: 4   Views: 42
Cellstyles display issue using addtable

Posted by Elisee9571  · 28-02-2025 - 09:27

$xlsx = new CreateXlsx();

        $cellStyles = array(
            'type' => 'general',
            'backgroundColor' => 'D9E1F2',
        );

        $contents = array(
            array(
                array(
                    'text' => '10',
                    'cellStyles' => $cellStyles,
                ),
                array(
                    'text' => '50',
                    'cellStyles' => $cellStyles,
                ),
                array(
                    'text' => '20',
                    'cellStyles' => $cellStyles,
                ),
            ),
            array(
                array(
                    'text' => '20',
                    'cellStyles' => $cellStyles,
                ),
                array(
                    'text' => '25.5',
                    'cellStyles' => $cellStyles,
                ),
                array(
                    'text' => '31',
                    'cellStyles' => $cellStyles,
                ),
            ),
        );
        
        $options = array(
            'columnNames' => array(
                array(
                    'text' => 'Values 1',
                    'bold' => true,
                    'color' => 'FFFFFF',
                    'cellStyles' => array(
                        'backgroundColor' => '4472C4',
                    ),
                ),
                array(
                    'text' => 'Values 2',
                    'bold' => true,
                    'color' => 'FFFFFF',
                    'cellStyles' => array(
                        'backgroundColor' => '4472C4',
                    ),
                ),
                array(
                    'text' => 'Values 3',
                    'bold' => true,
                    'color' => 'FFFFFF',
                    'cellStyles' => array(
                        'backgroundColor' => '4472C4',
                    ),
                ),
            ),
        );

        $xlsx->addTable($contents, 'B3', array(), $options);

        $fileName = Uuid::v7() . '.xlsx';
        $file = $this->getParameter('documents_dir') . '/' . $fileName;

        $xlsx->saveXlsx($file);

The issue i'm facing is related to CellStyles, everything that's related to the Contents CellStyles doesn't work. It's not displaying. More specifically, everything is working besides that, which means everything in the variable $options is working perfectly. The parameter cellStyles in the variable $contents isn't displaying. 

Posted by admin  · 28-02-2025 - 12:04

Hello,

What license and version of phpxlsx are you using?

Your username doesn't have any license. Please send to contact[at]phpdocx.com the username or email address that purchased the license you are using, and if you are using the classic or the namespaces package.

Regards.

Posted by Elisee9571  · 01-03-2025 - 18:40

Hi, 

I am using the version 4 of phpxlsx with the bureau licence.

We are using namespaces package.

I sent a mail with information required.

Posted by admin  · 01-03-2025 - 19:12

Hello,

Thanks for sending the requested information about the license you are using.
Please check your inbox. We have replied with all the needed information to use this option correctly, and with an update of the class.

Below you can read the same information sent by email:

With phpxlsx 4 (and also phpxlsx 3.5, phpxlsx 3, and phpxlsx 2.5), please apply the cellStyles using an array when adding cell contents with styles using addTable.
For example:

$xlsx = new CreateXlsx();

$cellStyles = array(
  'type' => 'general',
  'backgroundColor' => 'D9E1F2',
);

$contents = array(
  array(
    array(
      'text' => '10',
      ['cellStyles' => $cellStyles],
    ),
    array(
      'text' => '50',
      ['cellStyles' => $cellStyles],
    ),
    array(
      'text' => '20',
      ['cellStyles' => $cellStyles],
    ),
  ),
  array(
    array(
      'text' => '20',
      ['cellStyles' => $cellStyles],
    ),
    array(
      'text' => '25.5',
      ['cellStyles' => $cellStyles],
    ),
    array(
      'text' => '31',
      ['cellStyles' => $cellStyles],
    ),
  ),
);

$options = array(
  'columnNames' => array(
    array(
      'text' => 'Values 1',
      'bold' => true,
      'color' => 'FFFFFF',
      'cellStyles' => array(
        'backgroundColor' => '4472C4',
      ),
    ),
    array(
      'text' => 'Values 2',
      'bold' => true,
      'color' => 'FFFFFF',
      'cellStyles' => array(
        'backgroundColor' => '4472C4',
      ),
    ),
    array(
      'text' => 'Values 3',
      'bold' => true,
      'color' => 'FFFFFF',
      'cellStyles' => array(
        'backgroundColor' => '4472C4',
      ),
    ),
  ),
);

$xlsx->addTable($contents, 'B3', array(), $options);

$xlsx->saveXlsx('output');

As you can check in this sample script, instead of:

array(
  'text' => 'Cell content',
  'cellStyles' => $cellStyles,
),

please use an array to set cellStyles:

array(
  'text' => 'Cell content',
  ['cellStyles' => $cellStyles],
),

Using this code, everything will work as expected. Please note that this array must only be set in the $contents parameter of the addTable method, not in $options.

The next stable release of phpxlsx won't have this requirement/limitation/issue, and cell styles applied to cell contents using addTable will be applied as expected using:

array(
  'text' => 'Cell content',
  'cellStyles' => $cellStyles,
),

Regards.

Posted by Elisee9571  · 02-03-2025 - 10:12

hello, thanks the fact to do this has been taken into account and works.

 

array(

    'text' => 'Cell content',

    ['cellStyles' => $cellStyles],

),