4

我现在正在使用 PHPSpreadsheet,我正在尝试将行设置为自动高度。它在MSOffice中运行良好,但在LibreOffice中无法运行。

问题 如何在LibreOffice最新版本中使行自动高度工作。在MSOffice中运行良好。

自动高度

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

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

控制器

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

    }
}
4

1 回答 1

5

试试下面的代码

$spreadsheet->getActiveSheet()->getStyle('A1:D4')
    ->getAlignment()->setWrapText(true);
于 2018-11-05T05:33:42.423 回答