为什么duplicateStyle不适用于其他单元格?

时间:2015-10-23 09:29:43

标签: phpexcel

我在php源代码中创建了两个样式对象: - 第一种称为$ style1的样式是用红色填充单元格, - 第二种称为$ style2的样式是用绿色填充单元格, 但是当我使用duplicateStyle()方法在单元格中应用第一个样式时, 它应用了第二种风格。

这里的php源代码(最后一个PHPExcel版本:1.8.0):

<?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 landscape 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');

?>

那么问题是什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

使用$style1 = new PHPExcel_Style();代替$style1 = $sheet->getStyle('A1');

非常感谢你。