04_Printing.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
  5. use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  6. require __DIR__ . '/../Header.php';
  7. // Create new Spreadsheet object
  8. $helper->log('Create new Spreadsheet object');
  9. $spreadsheet = new Spreadsheet();
  10. // Set document properties
  11. $helper->log('Set document properties');
  12. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  13. ->setLastModifiedBy('Maarten Balliauw')
  14. ->setTitle('Office 2007 XLSX Test Document')
  15. ->setSubject('Office 2007 XLSX Test Document')
  16. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  17. ->setKeywords('office 2007 openxml php')
  18. ->setCategory('Test result file');
  19. // Add some data, we will use printing features
  20. $helper->log('Add some data');
  21. for ($i = 1; $i < 200; ++$i) {
  22. $spreadsheet->getActiveSheet()->setCellValue('A' . $i, $i);
  23. $spreadsheet->getActiveSheet()->setCellValue('B' . $i, 'Test value');
  24. }
  25. // Set header and footer. When no different headers for odd/even are used, odd header is assumed.
  26. $helper->log('Set header/footer');
  27. $spreadsheet->getActiveSheet()
  28. ->getHeaderFooter()
  29. ->setOddHeader('&L&G&C&HPlease treat this document as confidential!');
  30. $spreadsheet->getActiveSheet()
  31. ->getHeaderFooter()
  32. ->setOddFooter('&L&B' . $spreadsheet->getProperties()->getTitle() . '&RPage &P of &N');
  33. // Add a drawing to the header
  34. $helper->log('Add a drawing to the header');
  35. $drawing = new HeaderFooterDrawing();
  36. $drawing->setName('PhpSpreadsheet logo');
  37. $drawing->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png');
  38. $drawing->setHeight(36);
  39. $spreadsheet->getActiveSheet()
  40. ->getHeaderFooter()
  41. ->addImage($drawing, HeaderFooter::IMAGE_HEADER_LEFT);
  42. // Set page orientation and size
  43. $helper->log('Set page orientation and size');
  44. $spreadsheet->getActiveSheet()
  45. ->getPageSetup()
  46. ->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
  47. $spreadsheet->getActiveSheet()
  48. ->getPageSetup()
  49. ->setPaperSize(PageSetup::PAPERSIZE_A4);
  50. // Rename worksheet
  51. $helper->log('Rename worksheet');
  52. $spreadsheet->getActiveSheet()->setTitle('Printing');
  53. // Save
  54. $helper->write($spreadsheet, __FILE__);