Advertisement
noam76

export_excel.php

May 13th, 2023
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. <?php
  2. require_once 'vendor/autoload.php';
  3. require_once 'config.php';
  4.  
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  7.  
  8. $date = $_POST['date']; // Get the user-defined date from a form
  9.  
  10. // Fetch data from the "data_storage" table
  11. $stmt = $conn->prepare("SELECT * FROM data_storage WHERE DATE(created_at) = :date");
  12. $stmt->bindParam(':date', $date);
  13. $stmt->execute();
  14. $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  15.  
  16. $spreadsheet = new Spreadsheet();
  17. $sheet = $spreadsheet->getActiveSheet();
  18.  
  19. // Write data to the spreadsheet
  20. foreach ($data as $index => $row) {
  21.     $sheet->fromArray(json_decode($row['register_data'], true), null, "A" . ($index + 1));
  22. }
  23.  
  24. $writer = new Xlsx($spreadsheet);
  25. $filename = "data_export_" . $date . ".xlsx";
  26. $writer->save($filename);
  27.  
  28. header("Content-Type: application/vnd.ms-excel");
  29. header("Content-Disposition: attachment; filename=\"$filename\"");
  30. header("Cache-Control: max-age=0");
  31. readfile($filename);
  32. unlink($filename);
  33. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement