Workaround for copying style with PHPExcel

前端 未结 1 1428
不思量自难忘°
不思量自难忘° 2020-12-09 12:27

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->         


        
1条回答
  •  自闭症患者
    2020-12-09 13:12

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

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

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

    EDIT

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

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

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

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

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

    change to:

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

    EDIT #2

    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

    0 讨论(0)
提交回复
热议问题