PHPExcel Unable to Copy Style Object

I want to copy cell styles into an array and use

$arr = array();
$arr[] = $PHPExcel->getActiveSheet()->getStyle('A1');
$arr[] = $PHPExcel->getActiveSheet()->getStyle('B1');
$arr[] = $PHPExcel->getActiveSheet()->getStyle('C1');

//do smth .... 

$PHPExcel->getActiveSheet()->duplicateStyle($arr[0],'A2');
$PHPExcel->getActiveSheet()->duplicateStyle($arr[1],'B2');
$PHPExcel->getActiveSheet()->duplicateStyle($arr[2],'C2');

But all cells A2, B2, C2 have the same style as C1. What's wrong?

+4
source share
1 answer

Solution 1 . Use the latest version of PHPExcel from github

Solution 2 : Manually apply the patch to the duplicateStyle () method in the /PHPExcel/Worksheet.php classes from:

if ($this->_parent->cellXfExists($pCellStyle)) {
    // there is already this cell Xf in our collection
    $xfIndex = $pCellStyle->getIndex();
} else {
    // we don't have such a cell Xf, need to add
    $workbook->addCellXf($pCellStyle);
    $xfIndex = $pCellStyle->getIndex();
}

change to:

if ($existingStyle = $this->_parent->getCellXfByHashCode($pCellStyle-
>getHashCode())) {
    // there is already such cell Xf in our collection
    $xfIndex = $existingStyle->getIndex();
} else {
    // we don't have such a cell Xf, need to add
    $workbook->addCellXf($pCellStyle);
    $xfIndex = $pCellStyle->getIndex();
}
+1
source

Source: https://habr.com/ru/post/1649501/


All Articles