Javid
Javid

Reputation: 47

PHPExcel mergeCells every 2n column

I have some data in array $header1, $header2 and $data. The case is to insert all these arrays to excel file where header is already ready, that is:

enter image description here

Another headers will be set after inserting of $header1 and $header2. Now the main issue is to insert $header1 array to merged column:

enter image description here

Or first insert data and then merge them:

enter image description here

From my side I have tried a lot of solutions , but nothing...

MY CODE :

$excel2->setActiveSheetIndex(4);
$row = 6;
$col = 2;
$col_letter = 'D';
foreach($header1 as $key=>$value) {
    $excel2->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
    $col=$col+2;
    $merge = "'".$col_letter.$row.":".(++$col_letter).$row."'";
    $excel2->getActiveSheet()->mergeCells($merge);
    $col_letter++;
}

Take into account that $header1 array's data is unpredictable and the count of data can be over 50.

Any suggestion will be appreciated. Thanks.

Upvotes: 0

Views: 2639

Answers (1)

Javid
Javid

Reputation: 47

It Seems I have solved issue myself referring to PHPExcel Column Loop

$row = 26;
$col = 2;
$j = 'C';
foreach($hesablar as $key=>$value) {
    $excel2->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
    $col=$col+2;
    $i = $j++;
    $excel2->getActiveSheet()->mergeCells($i.$row.':'.$j.$row);
    echo $i.$row.':'.$j.$row.'<br />';
    $j++;
}

enter image description here

Upvotes: 1

Related Questions