2

I'm using this plugin, I know it's not the best, but I don't have time to rewrite all these scripts. I need to know how enable text wrap. The incomplete documentation on the website doesn't help at all.

I have tried using 1 and 'wrap' as the parameter in the array, but no luck.

Has anyone gotten this to work?

Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69
  • Does your cell contain contain actual line breaks ("\n") or are you simply expecting it to wrap on white space? BTW, setTextWrap() doesn't take any arguments – Mark Baker Aug 30 '12 at 07:57
  • No it doesn't contain line breaks. – Bird87 ZA Aug 30 '12 at 08:10
  • And it has to take arguments, because how will it know where the text wrap is required? – Bird87 ZA Aug 30 '12 at 08:11
  • 1
    If you look at the actual documentation for enabling word wrap, it doesn't take an argument. Word wrap is a boolean state (true or false) for a format, and setTextWrap() always sets it to TRUE... you can pass an argument, and it will simply be discarded. – Mark Baker Aug 30 '12 at 08:51
  • I see what you mean. As shown in the answer, I approached it the wrong way! :) – Bird87 ZA Aug 30 '12 at 10:18

4 Answers4

7

If You want to use construction addFormat(), name of wrapText property have to be 'wrap'.

You wrote: 'textWrap' => 1

you should: 'wrap' => true or 'wrap' => 1

for example:

$style = array(
    'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        'wrap' => true
    )
);
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($style);

the result for cell A1 is central alignment whith text wraping

kudis
  • 91
  • 1
  • 3
  • With the latest version of PHPExcel it's actually [`'textWrap' => true`](https://github.com/PHPOffice/PhpSpreadsheet/blob/9fdcaabe3cfe49e736e8e92a6700a756776407df/src/PhpSpreadsheet/Style/Alignment.php#L57) – Howdy_McGee Dec 07 '18 at 16:13
1

This code worked for me in PhpSpreadsheet

 $styleArray = [
            'borders'   => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN,
                ],
            ],
            'alignment' => [
                'wrapText' => true,
            ],
        ];

 $sheet->setCellValue('A1', 'product_name')->getStyle('A1')->applyFromArray($styleArray);
Akbarali
  • 688
  • 7
  • 16
0

Ok figured it out. I was trying to do it like this:

$format_caption =& $workbook->addFormat(array('size' => 11, 'fontFamily' => 'Calibri', 'numformat' => '@', 'bold' => 1, 'left' => 1, 'top' => 1, 'bottom' => 1, 'right' => 1, 'vAlign' => 'vcenter', 'align' => 'center', 'fgcolor' => 50, 'textWrap' => 1));

But setting 'textWrap' => 1 didn't work.

So I changed it a bit:

$format_caption =& $workbook->addFormat(array('size' => 11, 'fontFamily' => 'Calibri', 'numformat' => '@', 'bold' => 1, 'left' => 1, 'top' => 1, 'bottom' => 1, 'right' => 1, 'vAlign' => 'vcenter', 'align' => 'center', 'fgcolor' => 50));
$format_caption -> setTextWrap();

And that solved my issue.

Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69
0

textWrap not working bacause you set ROW height, ex:

$worksheet -> setRow($numRow, 17);

Remove this line, and height of row will be calculating automaticly.

VovaK
  • 1