65

I know that this line of code will make the cell text-wrap:

$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true);

'D1' being the chosen cell.

Instead of using this code for every cell I need wrapped, is there a way to make the entire Excel Worksheet automatically wrap everything?

Or is there a better practice technique to use for specified columns?

tehlivi
  • 790
  • 1
  • 12
  • 26

3 Answers3

131

Apply to a range:

$objPHPExcel->getActiveSheet()->getStyle('D1:E999')
    ->getAlignment()->setWrapText(true); 

Apply to a column

$objPHPExcel->getActiveSheet()->getStyle('D1:D'.$objPHPExcel->getActiveSheet()->getHighestRow())
    ->getAlignment()->setWrapText(true); 
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
34
$objPHPExcel->getDefaultStyle()->getAlignment()->setWrapText(true);
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
james
  • 341
  • 3
  • 2
  • 1
    Probably the best answer here. I don't know if this method was around when the question was posted, but it's definitely a nifty way to wrap text in all cells of the document. – Chris Sep 19 '21 at 07:24
  • yes, best answer to make automatically text-wrap all cell in doc regardless of any sheet, thanks – Birhan Karahasan Aug 27 '23 at 18:11
0

Apply to column

$highestRow = $$objPHPExcel->getActiveSheet()->getHighestRow();
for ($row = 1; $row <= $highestRow; $row++){
    $sheet->getStyle("D$row")->getAlignment()->setWrapText(true);
}
Naitik Shah
  • 513
  • 6
  • 13