38_Clone_worksheet.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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('PhpSpreadsheet Test Document')
  12. ->setSubject('PhpSpreadsheet Test Document')
  13. ->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
  14. ->setKeywords('office PhpSpreadsheet php')
  15. ->setCategory('Test result file');
  16. // Add some data
  17. $helper->log('Add some data');
  18. $spreadsheet->setActiveSheetIndex(0)
  19. ->setCellValue('A1', 'Hello')
  20. ->setCellValue('B2', 'world!')
  21. ->setCellValue('C1', 'Hello')
  22. ->setCellValue('D2', 'world!');
  23. // Miscellaneous glyphs, UTF-8
  24. $spreadsheet->setActiveSheetIndex(0)
  25. ->setCellValue('A4', 'Miscellaneous glyphs')
  26. ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
  27. $spreadsheet->getActiveSheet()->setCellValue('A8', "Hello\nWorld");
  28. $spreadsheet->getActiveSheet()->getRowDimension(8)->setRowHeight(-1);
  29. $spreadsheet->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true);
  30. // Rename worksheet
  31. $helper->log('Rename worksheet');
  32. $spreadsheet->getActiveSheet()->setTitle('Simple');
  33. // Clone worksheet
  34. $helper->log('Clone worksheet');
  35. $clonedSheet = clone $spreadsheet->getActiveSheet();
  36. $clonedSheet
  37. ->setCellValue('A1', 'Goodbye')
  38. ->setCellValue('A2', 'cruel')
  39. ->setCellValue('C1', 'Goodbye')
  40. ->setCellValue('C2', 'cruel');
  41. // Rename cloned worksheet
  42. $helper->log('Rename cloned worksheet');
  43. $clonedSheet->setTitle('Simple Clone');
  44. $spreadsheet->addSheet($clonedSheet);
  45. // Save
  46. $helper->write($spreadsheet, __FILE__);