问题
I'm using PHP Excel to create an Excel using a template Excel file. The problem is I have a datagrid and which I styled the header and first row in template. Here how it looks like:

The top leftmost coordinate is C49.
If I have 100 rows, I need to copy style of first row and paste it 100 times. Here is my code
$cstart = 2;
$rstart = 49;
$count = 1;
$input = $worksheet->getStyle(num2char($cstart) . $rstart);
foreach ($b_data['rawData'] as $value) {
$worksheet->setCellValueByColumnAndRow($cstart, $rstart, $count++)
->setCellValueByColumnAndRow($cstart + 1, $rstart, $value['key'])
->setCellValueByColumnAndRow($cstart + 5, $rstart, $value['value']);
$interval = num2char($cstart) . $rstart . ':' . num2char($cstart+5) . $rstart;
$worksheet->duplicateStyle($input, $interval);
$rstart++;
}
function num2char($num) {
$numeric = $num % 26;
$letter = chr(65 + $numeric);
$num2 = intval($num / 26);
if ($num2 > 0) {
return num2char($num2 - 1) . $letter;
} else {
return $letter;
}
}
However, I had the following:

but what I expected is:

Is it bug or am I doing something wrong?
回答1:
As Mark pointed out in the comments; a merged cell is structural, not style. So copying the style will not automatically copy the merged cells.
There is a feature request to be able to duplicate entire rows including merged cells
One way to deal with this is to check if the cells are part of a merge using the isInMergeRange()
function like this:
$workbook = new PHPExcel; // prepare the workbook
$sheet = $workbook->getActiveSheet(); // get the sheet
$sheet->mergeCells('A1:E1'); // merge some cells for tesing
$cell = $sheet->getCell('A1'); // get a cell to check if it is merged
$cell->isInMergeRange() // check if cell is merged
^This returns a Boolean value indicating if it is part of a merged cell.
Another function that may interest you is the isMergeRangeValueCell()
function:
$cell->isMergeRangeValueCell()
^This returns a Boolean value indicating it is part of a merged cell and the cell contains the value for the merged range; it returns false in all other situations.
来源:https://stackoverflow.com/questions/18800157/copy-and-paste-styles-from-one-row-to-other