Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write text between merged cells using phpExcel

Tags:

phpexcel

I have this code from phpExel.

$objPHPExcel->setActiveSheetIndex(1)->mergeCells('A1:I1');

This code creates some blank space between columns A1 to I1. Just want to add text in the center blank space(A1:I1).

I am trying this code:

$objPHPExcel->setActiveSheetIndex(1)->mergeCells('A1:I1','add some text here...');

but this code does not work. Can anyone help me please?

like image 399
Mr Alb Avatar asked May 04 '15 09:05

Mr Alb


People also ask

What is wrap text and merge in Excel?

Microsoft Excel can wrap text so it appears on multiple lines in a cell. You can format the cell so the text wraps automatically, or enter a manual line break. But this will prevent you from a cell potentially taking up too much room on your worksheet.

What is the difference between wrap text and merge cell?

If text is too long to be displayed in a single cell, the Spreadsheet allows you to wrap the text to make it display on multiple lines in the cell, or merge cells to combine two or more adjacent cells into a single larger/longer cell.

How do I merge rows in PhpExcel?

":I". ($row_count+4)); Where the variable $row_count has some unpredictable dynamic value like 25, 50, 75 and so on (no regular pattern). It merges the cells as shown in the preceding snap shot as can be seen immediately below the Note cell.


1 Answers

You simply write the text value that you want to the top-left cell in the merged cell range

$objPHPExcel->setActiveSheetIndex(1)
    ->mergeCells('A1:I1');
$objPHPExcel->getActiveSheet()
    ->getCell('A1')
    ->setValue('This is the text that I want to see in the merged cells');

EDIT

To centre the text, use

$objPHPExcel->getActiveSheet()
    ->getStyle('A1')
    ->getAlignment()
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

again, using the top-left cell of the range

like image 198
Mark Baker Avatar answered Oct 12 '22 01:10

Mark Baker