09_Pagebreaks.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  4. require __DIR__ . '/../Header.php';
  5. // Create new Spreadsheet object
  6. $helper->log('Create new Spreadsheet object');
  7. $spreadsheet = new Spreadsheet();
  8. // Set document properties
  9. $helper->log('Set document properties');
  10. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  11. ->setLastModifiedBy('Maarten Balliauw')
  12. ->setTitle('Office 2007 XLSX Test Document')
  13. ->setSubject('Office 2007 XLSX Test Document')
  14. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  15. ->setKeywords('office 2007 openxml php')
  16. ->setCategory('Test result file');
  17. // Create a first sheet
  18. $helper->log('Add data and page breaks');
  19. $spreadsheet->setActiveSheetIndex(0);
  20. $spreadsheet->getActiveSheet()->setCellValue('A1', 'Firstname')
  21. ->setCellValue('B1', 'Lastname')
  22. ->setCellValue('C1', 'Phone')
  23. ->setCellValue('D1', 'Fax')
  24. ->setCellValue('E1', 'Is Client ?');
  25. // Add data
  26. for ($i = 2; $i <= 50; ++$i) {
  27. $spreadsheet->getActiveSheet()->setCellValue('A' . $i, "FName $i");
  28. $spreadsheet->getActiveSheet()->setCellValue('B' . $i, "LName $i");
  29. $spreadsheet->getActiveSheet()->setCellValue('C' . $i, "PhoneNo $i");
  30. $spreadsheet->getActiveSheet()->setCellValue('D' . $i, "FaxNo $i");
  31. $spreadsheet->getActiveSheet()->setCellValue('E' . $i, true);
  32. // Add page breaks every 10 rows
  33. if ($i % 10 == 0) {
  34. // Add a page break
  35. $spreadsheet->getActiveSheet()->setBreak('A' . $i, Worksheet::BREAK_ROW);
  36. }
  37. }
  38. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  39. $spreadsheet->setActiveSheetIndex(0);
  40. $spreadsheet->getActiveSheet()->setTitle('Printing Options');
  41. // Set print headers
  42. $spreadsheet->getActiveSheet()
  43. ->getHeaderFooter()->setOddHeader('&C&24&K0000FF&B&U&A');
  44. $spreadsheet->getActiveSheet()
  45. ->getHeaderFooter()->setEvenHeader('&C&24&K0000FF&B&U&A');
  46. // Set print footers
  47. $spreadsheet->getActiveSheet()
  48. ->getHeaderFooter()->setOddFooter('&R&D &T&C&F&LPage &P / &N');
  49. $spreadsheet->getActiveSheet()
  50. ->getHeaderFooter()->setEvenFooter('&L&D &T&C&F&RPage &P / &N');
  51. // Save
  52. $helper->write($spreadsheet, __FILE__);