PHPSpreadsheet 自动行高不适用于 LibreOffice 最新版本
Posted
技术标签:
【中文标题】PHPSpreadsheet 自动行高不适用于 LibreOffice 最新版本【英文标题】:PHPSpreadsheet auto row height not works with LibreOffice latest version 【发布时间】:2018-03-16 15:45:00 【问题描述】:我现在正在使用 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;
【问题讨论】:
请注意this bug in Open/LibreOffice仍未修复.....PHPExcel无法修复 【参考方案1】:试试下面的代码
$spreadsheet->getActiveSheet()->getStyle('A1:D4')
->getAlignment()->setWrapText(true);
【讨论】:
以上是关于PHPSpreadsheet 自动行高不适用于 LibreOffice 最新版本的主要内容,如果未能解决你的问题,请参考以下文章