faltas_excel.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. $fecha = date('d_m_Y');
  3. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  4. header("Content-Disposition: attachment;filename=horario_$fecha.xlsx");
  5. header("Cache-Control: max-age=0");
  6. require_once "../vendor/autoload.php";
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Style\Border;
  9. use PhpOffice\PhpSpreadsheet\Style\Color;
  10. use PhpOffice\PhpSpreadsheet\Style\Fill;
  11. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. $spreadsheet = new Spreadsheet();
  14. $sheet = $spreadsheet->getActiveSheet();
  15. // Image settings
  16. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  17. $drawing->setName('La Salle')
  18. ->setDescription('La Salle')
  19. ->setPath('../imagenes/logo.png')
  20. ->setCoordinates('B1')
  21. ->setHeight(100)
  22. ->setOffsetX(10)
  23. ->setWorksheet($spreadsheet->getActiveSheet());
  24. $json = file_get_contents('php://input');
  25. $data = json_decode($json, true);
  26. empty($data) and die(json_encode(['error' => 'No se recibieron datos', 'data' => $data]));
  27. $data_excel = array(
  28. "CLAVE" => 'profesor_clave',
  29. "PROFESOR" => 'profesor_nombre',
  30. "CORREO" => 'profesor_correo',
  31. "FALTAS" => 'faltas',
  32. "TOTAL" => 'total',
  33. "PORCENTAJE" => 'porcentaje',
  34. ); // Same as before
  35. const ROW = 6;
  36. // Merge cells from A1 to C+ ROW
  37. $sheet->mergeCells('A1:B' . (ROW - 1));
  38. // Merge cells from D1 to size of $data_excel + 1
  39. $sheet->mergeCells('C1:' . chr(65 + count($data_excel) - 1) . (ROW - 1));
  40. // Set the title in D1 Sistema de Auditoría de Asistencia
  41. $sheet->setCellValue('C1', 'Sistema de Auditoría de Asistencia');
  42. $sheet->getStyle('C1')->applyFromArray([
  43. 'font' => [
  44. 'bold' => true,
  45. 'size' => 30,
  46. 'name' => 'Indivisa Text Sans',
  47. 'color' => ['argb' => '001d68'],
  48. ],
  49. 'alignment' => [
  50. 'vertical' => Alignment::VERTICAL_CENTER,
  51. ],
  52. ]);
  53. $lastColumnLetter = chr(65 + count($data_excel) - 1);
  54. $headers_range = 'A' . ROW . ':' . $lastColumnLetter . ROW;
  55. $keys = array_keys($data_excel);
  56. array_walk($keys, function ($key, $index) use ($sheet) {
  57. $sheet->setCellValue(chr(65 + $index) . ROW, $key);
  58. });
  59. // Apply the header styles
  60. $sheet->getStyle($headers_range)->applyFromArray([
  61. 'font' => [
  62. 'bold' => true,
  63. 'size' => 15,
  64. 'name' => 'Indivisa Text Sans',
  65. 'color' => ['argb' => Color::COLOR_WHITE],
  66. ],
  67. 'alignment' => [
  68. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  69. 'vertical' => Alignment::VERTICAL_CENTER,
  70. ],
  71. 'fill' => [
  72. 'fillType' => Fill::FILL_SOLID,
  73. 'startColor' => ['argb' => '001d68'],
  74. ]
  75. ]);
  76. // set filters
  77. $sheet->setAutoFilter($headers_range);
  78. // Styles that are common for all rows can be set outside the loop
  79. const DEFAULT_FONT = [
  80. 'size' => 12,
  81. 'name' => 'Indivisa Text Sans',
  82. 'color' => ['argb' => '001d68']
  83. ];
  84. const DEFAULT_STYLE = [
  85. 'alignment' => [
  86. 'vertical' => Alignment::VERTICAL_CENTER,
  87. 'wrapText' => true,
  88. ],
  89. 'font' => DEFAULT_FONT,
  90. 'borders' => [
  91. 'outline' => [
  92. 'borderStyle' => Border::BORDER_THIN,
  93. 'color' => ['argb' => Color::COLOR_WHITE],
  94. ]
  95. ]
  96. ];
  97. foreach ($data as $index => $registro) {
  98. $pair = $index % 2 == 0;
  99. $cellRange = 'A' . (ROW + $index + 1) . ':' . $lastColumnLetter . (ROW + $index + 1);
  100. $styleArray = DEFAULT_STYLE;
  101. $styleArray['fill'] = [
  102. 'fillType' => Fill::FILL_SOLID,
  103. 'startColor' => ['argb' => $pair ? 'd4d9dd' : 'f6f7f8'],
  104. ];
  105. $sheet->getStyle($cellRange)->applyFromArray($styleArray);
  106. $values = array_values($data_excel);
  107. array_walk($values, function ($row, $column_index) use ($sheet, $index, $registro) {
  108. $cellLocation = chr(65 + $column_index) . (ROW + $index + 1);
  109. $sheet->setCellValue($cellLocation, $registro[$row]);
  110. });
  111. }
  112. foreach ($sheet->getColumnIterator() as $column) {
  113. $sheet->getColumnDimension($column->getColumnIndex())->setAutoSize(true);
  114. }
  115. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  116. $writer->save('php://output');