diff --git a/Service/ExcelWriter.php b/Service/ExcelWriter.php index 64ce158..778a796 100644 --- a/Service/ExcelWriter.php +++ b/Service/ExcelWriter.php @@ -17,6 +17,7 @@ public function __construct(LoggerInterface $logger, PHPExcelFactory $phpExcel) parent::__construct($logger); $this->phpexcel = $phpExcel; $this->handle = $this->phpexcel->createPHPExcelObject(); + \PHPExcel_Shared_Font::setAutoSizeMethod(\PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT); } /** @@ -77,6 +78,7 @@ public function writeHeaders($sortedHeaders) $worksheet->setCellValue($cell, $header); // Assumption that all headers are multi-row, so we merge the rows of non-multirow headers $worksheet->mergeCells($column . $initRow . ':' . $column . ($initRow+1)); + $worksheet->getColumnDimension($column)->setAutoSize(true); $column++; } else { // This is a multi-row header, the first row consists of one value merged across several cells and the @@ -95,11 +97,14 @@ public function writeHeaders($sortedHeaders) // Now write the children's values onto the second row foreach ($header[$headerName] as $subHeaderName) { $worksheet->setCellValue($column . ($initRow + 1), $subHeaderName); + $worksheet->getColumnDimension($column)->setAutoSize(true); $column++; } } } + $worksheet->calculateColumnWidths(true); + $this->currentRow += 2; } diff --git a/Tests/Service/ExcelWriterTest.php b/Tests/Service/ExcelWriterTest.php index 559088f..24eb689 100644 --- a/Tests/Service/ExcelWriterTest.php +++ b/Tests/Service/ExcelWriterTest.php @@ -64,6 +64,9 @@ public function testShouldSetCurrentWorksheet() public function testShouldWriteSingleRowHeaders() { + $columnDimensionMock = $this->getMockBuilder('\PHPExcel_Worksheet_ColumnDimension')->disableOriginalConstructor()->getMock(); + $columnDimensionMock->expects($this->exactly(4))->method('setAutoSize')->with(true); + $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->exactly(4))->method('setCellValue')->withConsecutive( ['A1', 'Alpha'], ['B1', 'Bravo'], ['C1', 'Gamma'], ['D1', 'Delta'] @@ -71,6 +74,9 @@ public function testShouldWriteSingleRowHeaders() $worksheetMock->expects($this->exactly(4))->method('mergeCells')->withConsecutive( ['A1:A2'], ['B1:B2'], ['C1:C2'], ['D1:D2'] ); + $worksheetMock->expects($this->exactly(4))->method('getColumnDimension')->withConsecutive( + ['A'], ['B'], ['C'], ['D'] + )->willReturn($columnDimensionMock); $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $headers = ['Alpha', 'Bravo', 'Gamma', 'Delta']; @@ -79,6 +85,9 @@ public function testShouldWriteSingleRowHeaders() public function testShouldWriteNestedHeaders() { + $columnDimensionMock = $this->getMockBuilder('\PHPExcel_Worksheet_ColumnDimension')->disableOriginalConstructor()->getMock(); + $columnDimensionMock->expects($this->exactly(6))->method('setAutoSize')->with(true); + $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->exactly(7))->method('setCellValue')->withConsecutive( ['A1', 'Alpha'], ['B1', 'Bravo'], ['C1', 'Gamma'], ['D1', 'Delta'], ['E1', 'Echo'], @@ -87,6 +96,9 @@ public function testShouldWriteNestedHeaders() $worksheetMock->expects($this->exactly(5))->method('mergeCells')->withConsecutive( ['A1:A2'], ['B1:B2'], ['C1:C2'], ['D1:D2'], ['E1:F1'] ); + $worksheetMock->expects($this->exactly(6))->method('getColumnDimension')->withConsecutive( + ['A'], ['B'], ['C'], ['D'], ['E'], ['F'] + )->willReturn($columnDimensionMock); $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $headers = [0 => 'Alpha', 1 => 'Bravo', 2 => 'Gamma', 3 => 'Delta', 4 => ['Echo' => ['Foxtrot', 'Hotel']]];