xls_alumnoscarrera.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, 331, 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. if(!isset($_POST["generacion"], $_POST["carrera"])){
  27. echo "No se recibieron los datos necesarios.";
  28. exit();
  29. }
  30. $filter_carrera = filter_input(INPUT_POST, "carrera", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  31. //$filter_gen = fechaGuion(trim(filter_input(INPUT_POST, "generacion", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  32. $prefijo = (trim(filter_input(INPUT_POST, "prefijo", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  33. $query = "NULL, NULL, NULL, :carr, NULL, ";
  34. if(isset($_POST["estado"]) && $_POST["estado"]!=""){
  35. $query .= ":edo, ";
  36. $filter_estado = filter_input(INPUT_POST, "estado", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  37. }else{
  38. $query .= "NULL, ";
  39. }
  40. $query .= "0, NULL";
  41. $stmt = $pdo->prepare('Select * from fs_alumnobusca_carrera('.$query.') ORDER BY "Usuario_apellidos", "Usuario_nombre", "PlanEstudio_desc"');
  42. $stmt->bindParam(":carr", $filter_carrera);
  43. //$stmt->bindParam(":gen", $filter_gen);
  44. if(isset($filter_estado)) $stmt->bindParam(":edo", $filter_estado);
  45. if(!$stmt->execute()){
  46. $errorDesc = "Ocurrió un error al cargar los alumnos.";
  47. //print_r($stmt->errorInfo());
  48. }else{
  49. $alumnos_rs = $stmt->fetchAll();
  50. }
  51. $stmt->closeCursor();
  52. $stmt = null;
  53. //--------
  54. //
  55. // Create new Spreadsheet object
  56. $spreadsheet = new Spreadsheet();
  57. // Set document properties
  58. $spreadsheet->getProperties()->setCreator('Ingeniería La Salle')
  59. ->setLastModifiedBy('Ingeniería La Salle')
  60. ->setTitle('Alumnos por carrera')
  61. ->setDescription('Alumnos por carrera por generación.');
  62. $headerStyle = new Style();
  63. $headerStyle->applyFromArray(
  64. [
  65. 'fill' => [
  66. 'fillType' => Fill::FILL_SOLID,
  67. 'color' => ['argb' => 'FF001d68'],
  68. ],
  69. 'borders' => [
  70. 'bottom' => ['borderStyle' => Border::BORDER_THIN],
  71. 'right' => ['borderStyle' => Border::BORDER_MEDIUM],
  72. ],
  73. 'font' => [
  74. 'bold' => true,
  75. 'color' => ['argb' => 'FFFFFFFF'],
  76. ]
  77. ]
  78. );
  79. $titleStyle = new Style();
  80. $titleStyle->applyFromArray(
  81. [
  82. 'font' => [
  83. 'bold' => true,
  84. 'size' => 14,
  85. ]
  86. ]
  87. );
  88. //--------
  89. $sheet = 0;
  90. $spreadsheet->setActiveSheetIndex($sheet)->setTitle('Alumnos '.$prefijo);
  91. $spreadsheet->getActiveSheet()->setCellValueByColumnAndRow(1, 1, "Alumnos ".$prefijo);
  92. $spreadsheet->getActiveSheet()->mergeCells('A1:D1');
  93. $spreadsheet->getActiveSheet()->duplicateStyle($titleStyle, 'A1:D1');
  94. $row = 3;
  95. $col = 1;
  96. $spreadsheet->getActiveSheet()
  97. ->setCellValueByColumnAndRow($col++, $row, "Estado")
  98. ->setCellValueByColumnAndRow($col++, $row, "Subestado")
  99. ->setCellValueByColumnAndRow($col++, $row, "Clave")
  100. ->setCellValueByColumnAndRow($col++, $row, "Apellidos")
  101. ->setCellValueByColumnAndRow($col++, $row, "Nombre")
  102. ->setCellValueByColumnAndRow($col++, $row, "Generación")
  103. ->setCellValueByColumnAndRow($col++, $row, "CURP")
  104. ->setCellValueByColumnAndRow($col++, $row, "Correo")
  105. ->setCellValueByColumnAndRow($col++, $row, "Carrera")
  106. ->setCellValueByColumnAndRow($col++, $row, "Plan");
  107. $colMax = $col;
  108. $colLetter = numberToLetter($colMax-1);
  109. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A'.$row.':'.$colLetter.$row);
  110. $row++;
  111. $startRow = $row;
  112. foreach($alumnos_rs as $alumno){
  113. $col = 1;
  114. //$spreadsheet->setActiveSheetIndex($sheet)
  115. $correo = "";
  116. $stmt = $pdo->prepare('SELECT "Contacto_valor" from fs_contacto(:id, 3, NULL) WHERE "Contacto_valor" ILIKE \'%\' || \'lasallistas.org.mx\'');
  117. $stmt->bindParam(":id", $alumno["Usuario_id"]);
  118. if($stmt->execute()){
  119. $contacto_rs = $stmt->fetch();
  120. $correo = $contacto_rs["Contacto_valor"];
  121. }
  122. $stmt->closeCursor();
  123. if($correo == ""){
  124. $stmt = $pdo->prepare('SELECT "Contacto_valor" from fs_contacto(:id, 3, NULL) WHERE "Contacto_valor" NOT ILIKE \'%\' || \'lasallistas.org.mx\'');
  125. $stmt->bindParam(":id", $alumno["Usuario_id"]);
  126. if($stmt->execute()){
  127. $contacto_rs = $stmt->fetch();
  128. $correo = $contacto_rs["Contacto_valor"];
  129. }
  130. $stmt->closeCursor();
  131. }
  132. $spreadsheet->getActiveSheet()
  133. ->setCellValueByColumnAndRow($col++, $row, $alumno["EstadoAlumno_desc"])
  134. ->setCellValueByColumnAndRow($col++, $row, $alumno["SubEstadoAlumno_desc"])
  135. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_claveULSA"])
  136. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_apellidos"])
  137. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_nombre"])
  138. ->setCellValueByColumnAndRow($col++, $row, fechaMonthPicker($alumno["Alumno_generacion"]))
  139. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_curp"])
  140. ->setCellValueByColumnAndRow($col++, $row, $correo)
  141. ->setCellValueByColumnAndRow($col++, $row, $alumno["Carrera_desc"])
  142. ->setCellValueByColumnAndRow($col++, $row, $alumno["PlanEstudio_desc"]);
  143. $spreadsheet->getActiveSheet()->getStyle(numberToLetter(1).$row)->applyFromArray(['font' => [ 'color' => ['argb' => str_replace("#", "FF", $alumno["EstadoAlumno_color"])] ] ]);
  144. $spreadsheet->getActiveSheet()->getStyle(numberToLetter(3).$row)->getNumberFormat()->setFormatCode('000000');
  145. $row++;
  146. }
  147. $row--;
  148. //Auto ajustar anchos
  149. for($col = 1; $col < $colMax; $col++)
  150. $spreadsheet->getActiveSheet()->getColumnDimension(numberToLetter($col))->setAutoSize(true);
  151. //---------
  152. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  153. $spreadsheet->setActiveSheetIndex(0);
  154. // Redirect output to a client’s web browser (Xlsx)
  155. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  156. //header('Content-Disposition: attachment;filename="alumnos'.$prefijo.'_'.str_replace(array(',', ' '), array('',''), fechaMonthPicker($filter_gen)).'.xlsx"');
  157. header('Content-Disposition: attachment;filename="alumnos'.$prefijo.'.xlsx"');
  158. header('Cache-Control: max-age=0');
  159. // If you're serving to IE 9, then the following may be needed
  160. header('Cache-Control: max-age=1');
  161. // If you're serving to IE over SSL, then the following may be needed
  162. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  163. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  164. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  165. header('Pragma: public'); // HTTP/1.0
  166. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  167. $writer->save('php://output');
  168. exit;