xlsx improvements (#1518)

This commit is contained in:
Adrian Rudnik
2020-03-20 18:29:26 +01:00
committed by GitHub
parent b8c5323ece
commit fb2ce0d30a
3 changed files with 67 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ namespace App\Export\Base;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
class XlsxRenderer extends AbstractSpreadsheetRenderer
{
@@ -39,6 +40,36 @@ class XlsxRenderer extends AbstractSpreadsheetRenderer
throw new \Exception('Could not open temporary file');
}
// Store expensive calculations for later
$sheet = $spreadsheet->getActiveSheet();
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Enable auto filter for header row
$sheet->setAutoFilter('A1:' . $highestColumn . '1');
// Freeze first row and date & time columns for easier navigation
$sheet->freezePane('D2');
foreach (range('A', $highestColumn) as $column) {
// We default to a reasonable auto-width decided by the client,
// sadly ->getDefaultColumnDimension() is not supported so it needs
// to be specific about what column should be auto sized.
$col = $sheet->getColumnDimension($column);
// If no other width is specified (which defaults to -1)
if ($col->getWidth() === -1) {
$col->setAutoSize(true);
}
}
// Text inside cells should be top left
$sheet
->getStyle('A2:' . $highestColumn . $highestRow)
->getAlignment()
->setVertical(Alignment::VERTICAL_TOP)
->setHorizontal(Alignment::HORIZONTAL_LEFT);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($filename);