horario_excel.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. $ruta = "../";
  3. require_once "../vendor/autoload.php";
  4. require_once "../class/c_login.php";
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. $spreadsheet = new Spreadsheet();
  7. $sheet = $spreadsheet->getActiveSheet();
  8. //crea imagen
  9. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  10. $drawing->setName('La Salle');
  11. $drawing->setDescription('La Salle');
  12. $drawing->setPath('../imagenes/logo.png'); // put your path and image here
  13. $drawing->setCoordinates('A1');
  14. $drawing->setHeight(100);
  15. $drawing->setOffsetX(10);
  16. //agrega imagen
  17. $drawing->setWorksheet($spreadsheet->getActiveSheet());
  18. extract($_POST);
  19. $row = 6;
  20. $sábado = $sábado == 'true';
  21. $sheet->setCellValue("A$row", 'Hora');
  22. $sheet->setCellValue("B$row", 'Lunes');
  23. $sheet->setCellValue("C$row", 'Martes');
  24. $sheet->setCellValue("D$row", 'Miércoles');
  25. $sheet->setCellValue("E$row", 'Jueves');
  26. $sheet->setCellValue("F$row", 'Viernes');
  27. if ($sábado)
  28. $sheet->setCellValue("G$row", 'Sábado');
  29. // to this row set bold, font size 12 and Indivisa Sans, and center the text, bg color to #101097, color to white,
  30. $until = $sábado ? 'G' : 'F';
  31. $sheet->getStyle("A$row:$until$row")->getFont()->setBold(true);
  32. $sheet->getStyle("A$row:$until$row")->getFont()->setSize(15);
  33. $sheet->getStyle("A$row:$until$row")->getFont()->setName('Indivisa Display Sans');
  34. $sheet->getStyle("A$row:$until$row")->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  35. $sheet->getStyle("A$row:$until$row")->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
  36. $sheet->getStyle("A$row:$until$row")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
  37. $sheet->getStyle("A$row:$until$row")->getFill()->getStartColor()->setARGB('001d68');
  38. $sheet->getStyle("A$row:$until$row")->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
  39. # the first column is the hour of the day merged with 4 rows each
  40. $row++;
  41. foreach (range($min, $max) as $hour) {
  42. $sheet->setCellValue("A$row", "$hour:00");
  43. $sheet->mergeCells("A$row:A" . ($row + 3));
  44. for ($i = 0; $i < 4; $i++)
  45. $sheet->getRowDimension($row + $i)->setRowHeight(25);
  46. // align to center
  47. $sheet->getStyle("A$row")->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  48. $sheet->getStyle("A$row")->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
  49. $sheet->getStyle("A$row")->getFont()->setBold(true);
  50. $sheet->getStyle("A$row")->getFont()->setName('Indivisa Text Sans');
  51. $sheet->getStyle("A$row")->getFont()->setSize(12);
  52. $row += 4;
  53. }
  54. # Size columns to 30
  55. foreach (range('A', 'G') as $column) {
  56. if ($column == 'G' && !$sábado)
  57. continue;
  58. $sheet->getColumnDimension($column)->setWidth(($column == 'A' ? 10 : 30));
  59. }
  60. # foreach horarios
  61. $row = 7;
  62. $días = ['lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'];
  63. $lista_minutos = ['00', '15', '30', '45'];
  64. foreach (json_decode($horarios) as $horario) {
  65. [$hora, $minutos] = array_map(fn($x) => intval($x), explode(':', $horario->hora));
  66. $rowHora = $row + ($hora - $min) * 4;
  67. $rowHora += array_search($minutos, $lista_minutos);
  68. $colHora = array_search($horario->dia, $días) + 2;
  69. $content = new \PhpOffice\PhpSpreadsheet\RichText\RichText();
  70. $content->createText("$horario->hora");
  71. // $content = "$horario->hora $horario->materia\n$horario->salon\n$horario->profesor";
  72. $content->createTextRun(" $horario->materia\n")->getFont()->setBold(true)->setName('Indivisa Text Sans')->setSize(12);
  73. $content->createTextRun("Salón: $horario->salon\n")->getFont()->setBold(true);
  74. foreach ($horario->profesores as $profesor) $content->createTextRun("🎓 $profesor->profesor\n")->getFont()->setBold(true);
  75. // APPLY THE FONT FAMILY
  76. $sheet->setCellValueByColumnAndRow($colHora, $rowHora, $content);
  77. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  78. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
  79. // merge cells
  80. $sheet->mergeCellsByColumnAndRow($colHora, $rowHora, $colHora, $rowHora + $horario->bloques - 1);
  81. // set border white
  82. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
  83. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
  84. // set bg color d4d9dd
  85. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
  86. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFill()->getStartColor()->setARGB('d4d9dd');
  87. // set font size 12
  88. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFont()->setSize(12);
  89. // set font name Indivisa Text Sans
  90. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFont()->setName('Indivisa Text Sans');
  91. // set font color 001d68z
  92. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFont()->getColor()->setARGB('001d68');
  93. // wrap text
  94. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getAlignment()->setWrapText(true);
  95. // border white
  96. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
  97. $sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
  98. }
  99. #$writer = new Xlsx($spreadsheet);
  100. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  101. # $writer->save('asistencias.xlsx');
  102. // download
  103. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  104. header('Content-Disposition: attachment;filename="asistencias.xlsx"');
  105. header('Cache-Control: max-age=0');
  106. // cache expires in 60 seconds (1 minute)
  107. header('Expires: mon 26 jul 1997 05:00:00 gmt');
  108. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  109. header('Cache-Control: cache, must-revalidate');
  110. header('Pragma: public');
  111. $writer->save('php://output');