31_Document_properties_write.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Document\Properties;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. require __DIR__ . '/../Header.php';
  5. $inputFileType = 'Xlsx';
  6. $inputFileName = __DIR__ . '/../templates/31docproperties.xlsx';
  7. $spreadsheetReader = IOFactory::createReader($inputFileType);
  8. $callStartTime = microtime(true);
  9. $spreadsheet = $spreadsheetReader->load($inputFileName);
  10. $helper->logRead($inputFileType, $inputFileName, $callStartTime);
  11. $helper->log('Adjust properties');
  12. $spreadsheet->getProperties()->setTitle('Office 2007 XLSX Test Document')
  13. ->setSubject('Office 2007 XLSX Test Document')
  14. ->setDescription('Test XLSX document, generated using PhpSpreadsheet')
  15. ->setKeywords('office 2007 openxml php');
  16. // Save Excel 2007 file
  17. $filename = $helper->getFilename(__FILE__);
  18. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  19. $callStartTime = microtime(true);
  20. $writer->save($filename);
  21. $helper->logWrite($writer, $filename, $callStartTime);
  22. $helper->logEndingNotes();
  23. // Reread File
  24. $helper->log('Reread Xlsx file');
  25. $spreadsheetRead = IOFactory::load($filename);
  26. // Set properties
  27. $helper->log('Get properties');
  28. $helper->log('Core Properties:');
  29. $helper->log(' Created by - ' . $spreadsheet->getProperties()->getCreator());
  30. $helper->log(' Created on - ' . date('d-M-Y' . $spreadsheet->getProperties()->getCreated()) . ' at ' . date('H:i:s' . $spreadsheet->getProperties()->getCreated()));
  31. $helper->log(' Last Modified by - ' . $spreadsheet->getProperties()->getLastModifiedBy());
  32. $helper->log(' Last Modified on - ' . date('d-M-Y' . $spreadsheet->getProperties()->getModified()) . ' at ' . date('H:i:s' . $spreadsheet->getProperties()->getModified()));
  33. $helper->log(' Title - ' . $spreadsheet->getProperties()->getTitle());
  34. $helper->log(' Subject - ' . $spreadsheet->getProperties()->getSubject());
  35. $helper->log(' Description - ' . $spreadsheet->getProperties()->getDescription());
  36. $helper->log(' Keywords: - ' . $spreadsheet->getProperties()->getKeywords());
  37. $helper->log('Extended (Application) Properties:');
  38. $helper->log(' Category - ' . $spreadsheet->getProperties()->getCategory());
  39. $helper->log(' Company - ' . $spreadsheet->getProperties()->getCompany());
  40. $helper->log(' Manager - ' . $spreadsheet->getProperties()->getManager());
  41. $helper->log('Custom Properties:');
  42. $customProperties = $spreadsheet->getProperties()->getCustomProperties();
  43. foreach ($customProperties as $customProperty) {
  44. $propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
  45. $propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
  46. if ($propertyType == Properties::PROPERTY_TYPE_DATE) {
  47. $formattedValue = date('d-M-Y H:i:s', (int) $propertyValue);
  48. } elseif ($propertyType == Properties::PROPERTY_TYPE_BOOLEAN) {
  49. $formattedValue = $propertyValue ? 'TRUE' : 'FALSE';
  50. } else {
  51. $formattedValue = $propertyValue;
  52. }
  53. $helper->log(' ' . $customProperty . ' - (' . $propertyType . ') - ' . $formattedValue);
  54. }
  55. $helper->logEndingNotes();