3

i'm using v1.3.1 of phpspreadsheet.

i'm building an excel file (download) and everything's working fine. The only problem a have is to define the setPrintArea... It always returning me standards PrintArea into excel. Basicly, i build the excels lines and after define the printArea... I tried doing before too with no success.

$spreadsheet = IOFactory::load("template.xlsx");
$worksheet = $spreadsheet->getActiveSheet();

foreach ($rows AS $k => $v ) {
    $worksheet->getCell('F' . ($k + 3))->setValue($v);
    $worksheet->getCell('G' . ($k + 3))->setValue($v);
}

$worksheet->getPageSetup()->setPrintArea('A1:AG58');

...

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output'); //-- force download version

There's well 58 lines in the area but i cant get the set the area to the 'AG' cell... Basicly, i want to print all my excel in one page instead to 8.

Thx !

1 Answers1

4

setPrintArea() defines what to print but not how it should be printed. To have all columns A->AG and 58 rows appear on a single page you can use :

$worksheet->getPageSetup()->setFitToPage(true);

or to restrict by one dimension

$worksheet->getPageSetup()->setFitToWidth(1);
$worksheet->getPageSetup()->setFitToHeight(0);

https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#setting-printer-options-for-excel-files

  • How to know this code working or I misunderstand its purpose? After "$spreadsheet->getActiveSheet()->getPageSetup()->setFitToPage(true);", when I open the Excel > View > Page Layout, it shows 2 pages instead of 1 (my intension) – Coisox Feb 03 '23 at 07:31