I need to merge cells in Excel (xlsx) by rows and again by columns using PHPExcel. I tried the following.
$sheet->mergeCells(\"G\".($row_count+1).\":G\".($row
There is one more method for cell merging
/**
* Set merge on a cell range by using numeric cell coordinates
*
* @param int $pColumn1 Numeric column coordinate of the first cell
* @param int $pRow1 Numeric row coordinate of the first cell
* @param int $pColumn2 Numeric column coordinate of the last cell
* @param int $pRow2 Numeric row coordinate of the last cell
* @throws Exception
* @return PHPExcel_Worksheet
*/
public function mergeCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1)
Merging simply requires a valid range of cells like A1:B2, so your
$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));
should work without any problem.
Can you please experiment with a simple test case to prove that this is causing you a problem, and not something else in your script
EDIT
After rereading your question: Your problem may be that you're trying to merge cells that are already part of a merge range, rather than merging each row, then trying to merge by column, try merging the full rangein one go.
$sheet->mergeCells("G".($row_count+1).":I".($row_count+4));
function cellsToMergeByColsRow($start = -1, $end = -1, $row = -1){
$merge = 'A1:A1';
if($start>=0 && $end>=0 && $row>=0){
$start = PHPExcel_Cell::stringFromColumnIndex($start);
$end = PHPExcel_Cell::stringFromColumnIndex($end);
$merge = "$start{$row}:$end{$row}";
}
return $merge;
}
Addition to the case:
$objPHPExcel->getActiveSheet()->mergeCells(cellsToMergeByColsRow(0,2,3))
$sheet -> mergeCellsByColumnAndRow($col1, $row1, col2, row2);
is the function.
I make a simple function to calc cells to merge by cols and row.
function cellsToMergeByColsRow($start = NULL, $end = NULL, $row = NULL){
$merge = 'A1:A1';
if($start && $end && $row){
$start = PHPExcel_Cell::stringFromColumnIndex($start);
$end = PHPExcel_Cell::stringFromColumnIndex($end);
$merge = "$start{$row}:$end{$row}";
}
return $merge;
}
And call
$sheet->mergeCells(cellsToMergeByColsRow($col, $col+5, $row));
Thanks @Mark Baker
I was also looking solution for this question. where i want to merge cell and put content (value) on that. After few search i got some solution on this. but did not checked because i am using Maatawebsite for get Excel file.
But any one can try thing.. Solution is based on PHPExcel nit sure ,it will work on Maatawebsite.
Source Link
Merge from column A row 1 to column E row 1
$objPHPExcel->getActiveSheet()->mergeCells('A1:E1');
// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');
Merge from column A row 1 to column E row 3
$objPHPExcel->getActiveSheet()->mergeCells('A1:E3');
// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');
I checked maatawebsite document and they have same method mergeCells. so i think i would be work.
This Solution from Maatawebste.
$sheet->cells('A1:C1', function($cells) {
$cells->setBorder('thin', 'thin', 'thin', 'thin');
});
$sheet->mergeCells('A1:C1');
Solution 2nd
$sheet->setMergeColumn(array(
'columns' => array('A','B','C','D'),
'rows' => array(
array(2,3),
array(5,11),
)
));