使用PHPExcel复制样式的解决方法

使用PHPExcel复制样式的解决方法

本文介绍了使用PHPExcel复制样式的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将样式信息从单元格复制到范围,例如Excel中的Format Painter.该文档说要执行以下操作:

I want to copy the style information from cells to ranges, like Format Painter in Excel. The documentation says to do something like this:

$activeSheet->duplicateStyle($activeSheet->getStyle('A1'), 'D1:D100');
$activeSheet->duplicateStyle($activeSheet->getStyle('B1'), 'E1:E100');

似乎有一个错误,因为D1:D100和E1:E100都从单元格B1获得样式.如果更改两行的顺序,则两个范围都将从A1获得样式.同样,

There appears to be a bug because both D1:D100 and E1:E100 get the style from cell B1. If I change the order of the two lines, both ranges get the style from A1. Similarly,

$styleA = $activeSheet->getStyle('A1');
$styleB = $activeSheet->getStyle('B1');
$activeSheet->duplicateStyle($styleA, 'D1:D100');

导致D1:D100从单元格B1获取样式信息.最后的getStyle值用于所有重复的样式结果中.

results in D1:D100 getting style info from cell B1. The last getStyle value is used in all duplicateStyle results.

我确信PHPExcel的将来版本将有修复程序,在此之前,我只需要找出一种解决方法即可.

I'm sure that a future release of PHPExcel will have a fix, I just need to figure out a work-around until then.

推荐答案

一个轮回工作可能是使用xf索引样式:

One workround for you might be to use the style xf Indexes:

$xfIndex = $activeSheet->getCell('A1')->getXfIndex();

然后为该范围内所有单元格的xfIndex设置该值

Then to set that value for the xfIndex of all cells in the range

for ($col = 'D'; $col != 'E'; ++$col) {
    for ($row = 1; $row <= 100; ++$row) {
        $activeSheet->getCell($col . $row)->setXfIndex($xfIndex);
    }
}

编辑

或者,将修复程序应用于Classes/PHPExcel/Worksheet.php中的plicateStyle()方法

Alternatively, apply fix to the duplicateStyle() method in Classes/PHPExcel/Worksheet.php

第1479至1486行当前显示为:

lines 1479 to 1486 currently read:

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

类似地,在Classes/PHPExcel/Style.php中的applyFromArray()方法中

Similarly in the applyFromArray() method in Classes/PHPExcel/Style.php

第425行到432行当前显示为:

lines 425 to 432 currently read:

if ($workbook->cellXfExists($newStyle)) {
    // there is already such cell Xf in our collection
    $newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
} else {
    // we don't have such a cell Xf, need to add
    $workbook->addCellXf($newStyle);
    $newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
}

更改为:

if ($existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode())) {
    // there is already such cell Xf in our collection
    $newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
} else {
    // we don't have such a cell Xf, need to add
    $workbook->addCellXf($newStyle);
    $newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
}

编辑#2

修复现在已推送到github上的developer分支.根据使用的样式数量,它确实会给性能带来轻微的影响.明天晚上我会尝试获得更快的版本

Fix has now been pushed to the develop branch on github. It does give a slight performance hit, depending on the number of styles in use... I'll try and get a faster version tomorrow night

这篇关于使用PHPExcel复制样式的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 14:16