Why duplicateStyle doesn't work for another cell?

Topics: User Forum
Oct 23, 2015 at 2:15 PM
Hello,

Im making an export for my company which takes soma data, given through PHP, and outputs them to a excel spreadsheet.

Everything works well, except for one little thing. Im conditionally formatting some cells to have a specific color. Neither I get an error, nor i get the right background color for the other cells, they just stay white.

I'm using PHPExcel to create the output, the following is my Code:
<?php

// Import PHPExcel class:
include("./PHPExcel/Classes/PHPExcel.php");

// Create a workbook :
$workbook = new PHPExcel;

// Create a sheet :
$sheet = $workbook->getActiveSheet();

// Set to A4 page size :
$sheet->getPageSetup()->setPaperSize(9);

// Set to landcape orientation, but it doesn't work :
$sheet->getPageSetup()->setOrientation("landscape");

/*Create style1 for A1 cell :*/
$style1 = $sheet->getStyle('A1');

$style1->applyFromArray(

    array(
            'fill' => array(
                                'type' => 'solid', 
                                //Color filling :
                                'color' => array('rgb' => 'ff0000')
                            )
        )
);

/*Create style2 for B1 cell :*/
$style2 = $sheet->getStyle('B1');

$style2->applyFromArray(

    array(
            'fill' => array(
                                'type' => 'solid', 
                                //Green filling :
                                'color' => array('rgb' => '00ff00')
                            )
        )
);

//Duplicate style1 to A2 :
$sheet->duplicateStyle($style1, "A2");

//Write in A2 cell. Bug : red filling doesn't work :
$sheet->setCellValue("A2", "Must be red");


//Duplicate style2 to B2 :
$sheet->duplicateStyle($style2, "B2");

//Write in B2 cell :
$sheet->setCellValue("B2", "Must be green");

//Output to brower :
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
header('Content-Disposition: attachment;filename="file.xlsx"'); 
header('Cache-Control: max-age=0'); 


$writer = new PHPExcel_Writer_Excel2007($workbook);

//Office 2003 compatibilty :
$writer->setOffice2003Compatibility(true);
$writer->save('php://output');

?>
So What's the problem ? Any Idea ? Thank you in advance.
Marked as answer by totoAussi on 10/23/2015 at 6:49 AM
Oct 23, 2015 at 2:51 PM
I find the problem :

Use $style1 = new PHPExcel_Style(); instead of $style1 = $sheet->getStyle('A1');

Thank you very much.