xls_alumnos.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. setlocale(LC_TIME, 'es_MX.UTF-8');
  3. require_once("../../include/nocache.php");
  4. require_once("../../include/constantes.php");
  5. require_once("../../include/bd_pdo.php");
  6. require_once("../../include/util.php");
  7. require_once("../../classes/ValidaSesion.php");
  8. require "../../include/phpSpreadsheet/autoload.php";
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Style\Border;
  12. use PhpOffice\PhpSpreadsheet\Style\Fill;
  13. use PhpOffice\PhpSpreadsheet\Style\Style;
  14. use PhpOffice\PhpSpreadsheet\Shared\Date;
  15. function numberToLetter ($number){
  16. $temp = ($number-1) % 26;
  17. return chr($temp + 65);
  18. };
  19. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  20. $objSesion = new ValidaSesion($pdo, 101, GEMA);
  21. if(!$objSesion->tieneAcceso()){
  22. echo "No tienes permiso de ver esta página. Revisa que tu sesión siga activa.";
  23. exit();
  24. }
  25. unset($objValida);
  26. //--------
  27. //
  28. // Create new Spreadsheet object
  29. $spreadsheet = new Spreadsheet();
  30. // Set document properties
  31. $spreadsheet->getProperties()->setCreator('Ingeniería La Salle')
  32. ->setLastModifiedBy('Ingeniería La Salle')
  33. ->setTitle('Estado de Alumnos')
  34. ->setDescription('Estado actual de alumnos.');
  35. $headerStyle = new Style();
  36. $headerStyle->applyFromArray(
  37. [
  38. 'fill' => [
  39. 'fillType' => Fill::FILL_SOLID,
  40. 'color' => ['argb' => 'FF001d68'],
  41. ],
  42. 'borders' => [
  43. 'bottom' => ['borderStyle' => Border::BORDER_THIN],
  44. 'right' => ['borderStyle' => Border::BORDER_MEDIUM],
  45. ],
  46. 'font' => [
  47. 'bold' => true,
  48. 'color' => ['argb' => 'FFFFFFFF'],
  49. ]
  50. ]
  51. );
  52. //--------
  53. $stmt = $pdo->prepare('Select * from "Alumno_view" where "Nivel_id"=:nivel AND "EstadoAlumno_id"=1');//sólo activos
  54. $stmt->bindParam(":nivel", $_SESSION["nivel_id"]);
  55. if(!$stmt->execute()){
  56. echo "Ocurrió un error al cargar los alumnos.";
  57. //print_r($stmt->errorInfo());
  58. exit();
  59. }
  60. $alumno_rs = $stmt->fetchAll();
  61. $stmt->closeCursor();
  62. $spreadsheet->setActiveSheetIndex(0)->setTitle('Alumnos ');
  63. $row = 1;
  64. $col = 1;
  65. $spreadsheet->getActiveSheet()
  66. ->setCellValueByColumnAndRow($col++, $row, "Estado")
  67. ->setCellValueByColumnAndRow($col++, $row, "Subestado")
  68. ->setCellValueByColumnAndRow($col++, $row, "Clave")
  69. ->setCellValueByColumnAndRow($col++, $row, "Apellidos")
  70. ->setCellValueByColumnAndRow($col++, $row, "Nombre")
  71. ->setCellValueByColumnAndRow($col++, $row, "Generación")
  72. ->setCellValueByColumnAndRow($col++, $row, "CURP")
  73. ->setCellValueByColumnAndRow($col++, $row, "Carrera")
  74. ->setCellValueByColumnAndRow($col++, $row, "Plan");
  75. $colMax = $col;
  76. $colLetter = numberToLetter($colMax-1);
  77. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A1:'.$colLetter.'1');
  78. $row = 2;
  79. $startRow = $row;
  80. foreach($alumno_rs as $alumno){
  81. $col = 1;
  82. //$spreadsheet->setActiveSheetIndex($sheet)
  83. $spreadsheet->getActiveSheet()
  84. ->setCellValueByColumnAndRow($col++, $row, $alumno["EstadoAlumno_desc"])
  85. ->setCellValueByColumnAndRow($col++, $row, $alumno["SubEstadoAlumno_desc"])
  86. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_claveULSA"])
  87. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_apellidos"])
  88. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_nombre"])
  89. ->setCellValueByColumnAndRow($col++, $row, fechaMonthPicker($alumno["Alumno_generacion"]))
  90. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_curp"])
  91. ->setCellValueByColumnAndRow($col++, $row, $alumno["Carrera_desc"])
  92. ->setCellValueByColumnAndRow($col++, $row, $alumno["PlanEstudio_desc"]);
  93. $spreadsheet->getActiveSheet()->getStyle(numberToLetter(1).$row)->applyFromArray(['font' => [ 'color' => ['argb' => str_replace("#", "FF", $alumno["EstadoAlumno_color"])] ] ]);
  94. $spreadsheet->getActiveSheet()->getStyle(numberToLetter(3).$row)->getNumberFormat()->setFormatCode('000000');
  95. $row++;
  96. }
  97. $row--;
  98. //Auto ajustar anchos
  99. for($col = 1; $col < $colMax; $col++)
  100. $spreadsheet->getActiveSheet()->getColumnDimension(numberToLetter($col))->setAutoSize(true);
  101. //---------
  102. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  103. $spreadsheet->setActiveSheetIndex(0);
  104. // Redirect output to a client’s web browser (Xlsx)
  105. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  106. header('Content-Disposition: attachment;filename="alumnos_periodo.xlsx"');
  107. header('Cache-Control: max-age=0');
  108. // If you're serving to IE 9, then the following may be needed
  109. header('Cache-Control: max-age=1');
  110. // If you're serving to IE over SSL, then the following may be needed
  111. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  112. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  113. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  114. header('Pragma: public'); // HTTP/1.0
  115. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  116. $writer->save('php://output');
  117. exit;