Files
paad/export/horario_excel.php

140 lines
6.4 KiB
PHP

<?php
$ruta = "../";
require_once "../vendor/autoload.php";
require_once "../class/c_login.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//crea imagen
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('La Salle');
$drawing->setDescription('La Salle');
$drawing->setPath('../imagenes/logo.png'); // put your path and image here
$drawing->setCoordinates('A1');
$drawing->setHeight(100);
$drawing->setOffsetX(10);
//agrega imagen
$drawing->setWorksheet($spreadsheet->getActiveSheet());
extract($_POST);
$row = 6;
$sábado = $sábado == 'true';
$sheet->setCellValue("A$row", 'Hora');
$sheet->setCellValue("B$row", 'Lunes');
$sheet->setCellValue("C$row", 'Martes');
$sheet->setCellValue("D$row", 'Miércoles');
$sheet->setCellValue("E$row", 'Jueves');
$sheet->setCellValue("F$row", 'Viernes');
if ($sábado)
$sheet->setCellValue("G$row", 'Sábado');
// to this row set bold, font size 12 and Indivisa Sans, and center the text, bg color to #101097, color to white,
$until = $sábado ? 'G' : 'F';
$sheet->getStyle("A$row:$until$row")->getFont()->setBold(true);
$sheet->getStyle("A$row:$until$row")->getFont()->setSize(15);
$sheet->getStyle("A$row:$until$row")->getFont()->setName('Indivisa Display Sans');
$sheet->getStyle("A$row:$until$row")->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$sheet->getStyle("A$row:$until$row")->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$sheet->getStyle("A$row:$until$row")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
$sheet->getStyle("A$row:$until$row")->getFill()->getStartColor()->setARGB('001d68');
$sheet->getStyle("A$row:$until$row")->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
# the first column is the hour of the day merged with 4 rows each
$row++;
foreach (range($min, $max) as $hour) {
$sheet->setCellValue("A$row", "$hour:00");
$sheet->mergeCells("A$row:A" . ($row + 3));
for ($i = 0; $i < 4; $i++)
$sheet->getRowDimension($row + $i)->setRowHeight(25);
// align to center
$sheet->getStyle("A$row")->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$sheet->getStyle("A$row")->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$sheet->getStyle("A$row")->getFont()->setBold(true);
$sheet->getStyle("A$row")->getFont()->setName('Indivisa Text Sans');
$sheet->getStyle("A$row")->getFont()->setSize(12);
$row += 4;
}
# Size columns to 30
foreach (range('A', 'G') as $column) {
if ($column == 'G' && !$sábado)
continue;
$sheet->getColumnDimension($column)->setWidth(($column == 'A' ? 10 : 30));
}
# foreach horarios
$row = 7;
$días = ['lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'];
$lista_minutos = ['00', '15', '30', '45'];
foreach (json_decode($horarios) as $horario) {
[$hora, $minutos] = array_map(fn($x) => intval($x), explode(':', $horario->hora));
$rowHora = $row + ($hora - $min) * 4;
$rowHora += array_search($minutos, $lista_minutos);
$colHora = array_search($horario->dia, $días) + 2;
$content = new \PhpOffice\PhpSpreadsheet\RichText\RichText();
$content->createText("$horario->hora");
// $content = "$horario->hora $horario->materia\n$horario->salon\n$horario->profesor";
$content->createTextRun(" $horario->materia\n")->getFont()->setBold(true)->setName('Indivisa Text Sans')->setSize(12);
$content->createTextRun("Salón: $horario->salon\n")->getFont()->setBold(true);
foreach ($horario->profesores as $profesor) $content->createTextRun("🎓 $profesor->profesor\n")->getFont()->setBold(true);
// APPLY THE FONT FAMILY
$sheet->setCellValueByColumnAndRow($colHora, $rowHora, $content);
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
// merge cells
$sheet->mergeCellsByColumnAndRow($colHora, $rowHora, $colHora, $rowHora + $horario->bloques - 1);
// set border white
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
// set bg color d4d9dd
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFill()->getStartColor()->setARGB('d4d9dd');
// set font size 12
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFont()->setSize(12);
// set font name Indivisa Text Sans
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFont()->setName('Indivisa Text Sans');
// set font color 001d68z
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getFont()->getColor()->setARGB('001d68');
// wrap text
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getAlignment()->setWrapText(true);
// border white
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$sheet->getStyleByColumnAndRow($colHora, $rowHora)->getBorders()->getOutline()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
}
#$writer = new Xlsx($spreadsheet);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
# $writer->save('asistencias.xlsx');
// download
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="asistencias.xlsx"');
header('Cache-Control: max-age=0');
// cache expires in 60 seconds (1 minute)
header('Expires: mon 26 jul 1997 05:00:00 gmt');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer->save('php://output');