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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用PHPExcel复制样式的解决方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将样式信息从单元格复制到范围,例如Excel中的Format Painter.文档说要做这样的事情:
$activeSheet->duplicatestyle($activeSheet->getStyle('A1'),'D1:D100');
$activeSheet->duplicateStyle($activeSheet->getStyle('B1'),'E1:E100');

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

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

得到D1:D100从单元格B1获取样式信息.最后的getStyle值用于所有duplicateStyle结果.

我确信PHPExcel的未来版本将有一个修复,我只需要找到一个解决方法,直到那时.

一个workround for you可能是使用样式xf索引:
$xfIndex = $activeSheet->getCell('A1')->getXfIndex();

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

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

编辑

或者,将修复应用于Classes / PHPExcel / Worksheet.PHP中的duplicateStyle()方法

目前阅读第1479至1486行:

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()方法

第425至432行目前已阅读:

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

Fix现已被推送到gIThub上的develop分支.根据使用的样式数量,它确实会有轻微的性能影响……我会尝试明天晚上获得更快的版本

脚本宝典总结

以上是脚本宝典为你收集整理的使用PHPExcel复制样式的解决方法全部内容,希望文章能够帮你解决使用PHPExcel复制样式的解决方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。