30_Template.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\IOFactory;
  3. use PhpOffice\PhpSpreadsheet\Shared\Date;
  4. require __DIR__ . '/../Header.php';
  5. $helper->log('Load from Xls template');
  6. $reader = IOFactory::createReader('Xls');
  7. $spreadsheet = $reader->load(__DIR__ . '/../templates/30template.xls');
  8. $helper->log('Add new data to the template');
  9. $data = [['title' => 'Excel for dummies',
  10. 'price' => 17.99,
  11. 'quantity' => 2,
  12. ],
  13. ['title' => 'PHP for dummies',
  14. 'price' => 15.99,
  15. 'quantity' => 1,
  16. ],
  17. ['title' => 'Inside OOP',
  18. 'price' => 12.95,
  19. 'quantity' => 1,
  20. ],
  21. ];
  22. $spreadsheet->getActiveSheet()->setCellValue('D1', Date::PHPToExcel(time()));
  23. $baseRow = 5;
  24. foreach ($data as $r => $dataRow) {
  25. $row = $baseRow + $r;
  26. $spreadsheet->getActiveSheet()->insertNewRowBefore($row, 1);
  27. $spreadsheet->getActiveSheet()->setCellValue('A' . $row, $r + 1)
  28. ->setCellValue('B' . $row, $dataRow['title'])
  29. ->setCellValue('C' . $row, $dataRow['price'])
  30. ->setCellValue('D' . $row, $dataRow['quantity'])
  31. ->setCellValue('E' . $row, '=C' . $row . '*D' . $row);
  32. }
  33. $spreadsheet->getActiveSheet()->removeRow($baseRow - 1, 1);
  34. // Save
  35. $helper->write($spreadsheet, __FILE__);