11_Documentsecurity.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../Header.php';
  4. // Create new Spreadsheet object
  5. $helper->log('Create new Spreadsheet object');
  6. $spreadsheet = new Spreadsheet();
  7. // Set document properties
  8. $helper->log('Set document properties');
  9. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  10. ->setLastModifiedBy('Maarten Balliauw')
  11. ->setTitle('Office 2007 XLSX Test Document')
  12. ->setSubject('Office 2007 XLSX Test Document')
  13. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  14. ->setKeywords('office 2007 openxml php')
  15. ->setCategory('Test result file');
  16. // Add some data
  17. $helper->log('Add some data');
  18. $spreadsheet->setActiveSheetIndex(0);
  19. $spreadsheet->getActiveSheet()->setCellValue('A1', 'Hello');
  20. $spreadsheet->getActiveSheet()->setCellValue('B2', 'world!');
  21. $spreadsheet->getActiveSheet()->setCellValue('C1', 'Hello');
  22. $spreadsheet->getActiveSheet()->setCellValue('D2', 'world!');
  23. // Rename worksheet
  24. $helper->log('Rename worksheet');
  25. $spreadsheet->getActiveSheet()->setTitle('Simple');
  26. // Set document security
  27. $helper->log('Set document security');
  28. $spreadsheet->getSecurity()->setLockWindows(true);
  29. $spreadsheet->getSecurity()->setLockStructure(true);
  30. $spreadsheet->getSecurity()->setWorkbookPassword('PhpSpreadsheet');
  31. // Set sheet security
  32. $helper->log('Set sheet security');
  33. $spreadsheet->getActiveSheet()->getProtection()->setPassword('PhpSpreadsheet');
  34. $spreadsheet->getActiveSheet()->getProtection()->setSheet(true); // This should be enabled in order to enable any of the following!
  35. $spreadsheet->getActiveSheet()->getProtection()->setSort(true);
  36. $spreadsheet->getActiveSheet()->getProtection()->setInsertRows(true);
  37. $spreadsheet->getActiveSheet()->getProtection()->setFormatCells(true);
  38. // Save
  39. $helper->write($spreadsheet, __FILE__);