PHPExcel无法复制Style对象

时间:2016-07-29 08:10:35

标签: phpexcel

我想将单元格样式复制到数组而不是使用

$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');

但所有单元格A2,B2,C2都与C1相同。 怎么了?

1 个答案:

答案 0 :(得分:1)

解决方案1 ​​:请使用github中的最新版PHPExcel

解决方案2 : 手动将修复应用于 Classes / PHPExcel / Worksheet.php 中的 duplicateStyle()方法 从:

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();
}

更改为:

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();
}