Copy and paste styles from one line to another

I am using PHP Excel to create Excel using an Excel Excel file. The problem is that I have a datagrid and I created the header and first line in the template. Here's what it looks like:

template

The upper left coordinate is C49.

If I have 100 lines, I need to copy the style of the first line 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:

enter image description here

but I was expecting:

enter image description here

Is this a mistake or am I doing something wrong?

+7
php phpexcel
source share
1 answer

As Mark noted in the comments; a merged cell is structural, not stylish. Therefore, copying a style will not automatically copy the merged cells.

There is a function request to be able to duplicate whole rows, including merged cells


One way to handle this is to check if the cells are part of the merge using the isInMergeRange() function as follows:

 $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 whether it is part of a merged cell.


Another function that might interest you is the isMergeRangeValueCell() function:

 $cell->isMergeRangeValueCell() 

^ This returns a boolean value indicating that it is part of a merged cell and , the cell contains a value for the merged range; it returns false in all other situations.

+1
source share

All Articles