PHPExcel and Text Wrapping

PhpMysqlDatabasePhpexcel

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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); 

Solution 2 - Php

$objPHPExcel->getDefaultStyle()->getAlignment()->setWrapText(true);

Solution 3 - Php

Apply to column

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontehliviView Question on Stackoverflow
Solution 1 - PhpMark BakerView Answer on Stackoverflow
Solution 2 - PhpjamesView Answer on Stackoverflow
Solution 3 - PhpNaitik ShahView Answer on Stackoverflow