PHPSpreadsheet auto row height not works with LibreOffice latest version

Mr. ED picture Mr. ED · Oct 5, 2017 · Viewed 9.7k times · Source

I am using PHPSpreadsheet now, I am trying to get rows to auto height. It is working fine in MSOffice But Not work in LibreOffice.

Question How to make the row auto height work in LibreOffice latest version. Works fine in MSOffice.

Autoheight

$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) { 
    $rowID->setRowHeight(-1); 
}

Controller

<?php

require(APPPATH . 'vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Events extends MX_Controller {

    public function test() {
        $spreadsheet = new Spreadsheet();

        $spreadsheet->getProperties()->setCreator('')
                ->setLastModifiedBy('')
                ->setTitle('')
                ->setSubject('')
                ->setDescription('');

        $spreadsheet->getDefaultStyle()->getFont()->setName('Arial');
        $spreadsheet->getDefaultStyle()->getFont()->setSize(24);

        foreach(range('A','B') as $columnID) {
            $spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
        }

        $spreadsheet->getActiveSheet()->getStyle('A')->getAlignment()->setWrapText(true);

        $spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) { 
            $rowID->setRowHeight(-1); 
        }

        $spreadsheet->setActiveSheetIndex(0)
                ->setCellValue("A1",'Firstname')
                ->setCellValue("B1",'Lastname')
                ->setCellValue("A2",'John')
                ->setCellValue("B2",'Doe');


        $spreadsheet->getActiveSheet()->setTitle('Users Information');

        $spreadsheet->setActiveSheetIndex(0);

        /* Here there will be some code where you create $spreadsheet */

        // redirect output to client browser
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="myfile.xls"');
        header('Cache-Control: max-age=0');

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
        $writer->save('php://output');

        exit;

    }
}

Answer

andresflorez12 picture andresflorez12 · Nov 5, 2018

Try the following code

$spreadsheet->getActiveSheet()->getStyle('A1:D4')
    ->getAlignment()->setWrapText(true);