xls_cambiocarrera.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. \PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );
  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, 125, 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["plan_o"], $_POST["plan_d"]) && isset($_POST["fini"], $_POST["ffin"])) {
  27. $filter_plan_o = filter_input(INPUT_POST, "plan_o", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  28. $filter_plan_d = filter_input(INPUT_POST, "plan_d", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  29. $filter_fini = fechaGuion(trim(filter_input(INPUT_POST, "fini", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  30. $filter_ffin = fechaGuion(trim(filter_input(INPUT_POST, "ffin", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  31. }else{
  32. echo "No se recibieron los datos.";
  33. exit();
  34. }
  35. $query = "";
  36. if($filter_plan_o != 0){
  37. $query .= ":plan_o,";
  38. }else
  39. $query .= "NULL,";
  40. if($filter_plan_d != 0){
  41. $query .= ":plan_d,";
  42. }else
  43. $query .= "NULL,";
  44. $stmt = $pdo->prepare('Select * from fs_alumnocarrera_cambio('.$query.' :fini, :ffin)');
  45. $stmt->bindParam(":fini", $filter_fini);
  46. $stmt->bindParam(":ffin", $filter_ffin);
  47. if($filter_plan_o != 0){ $stmt->bindParam(":plan_o", $filter_plan_o); }
  48. if($filter_plan_d != 0){ $stmt->bindParam(":plan_d", $filter_plan_d); }
  49. if(!$stmt->execute()){
  50. echo "Ocurrió un error al cargar los cambios de carrera.";
  51. //print_r($stmt->errorInfo());
  52. exit();
  53. }else{
  54. $cambio_rs = $stmt->fetchAll();
  55. }
  56. $stmt->closeCursor();
  57. if($filter_plan_o != 0){
  58. $stmt = $pdo->prepare('Select * from fs_alumnoprocedencia(:plan_d, :fini, :ffin)');
  59. $stmt->bindParam(":plan_d", $filter_plan_d);
  60. }else{
  61. $stmt = $pdo->prepare('Select * from fs_alumnoprocedencia(NULL, :fini, :ffin)');
  62. }
  63. $stmt->bindParam(":fini", $filter_fini);
  64. $stmt->bindParam(":ffin", $filter_ffin);
  65. if(!$stmt->execute()){
  66. echo "Ocurrió un error al cargar las instituciones de procedencia.";
  67. //print_r($stmt->errorInfo());
  68. exit();
  69. }else{
  70. $procedencia_rs = $stmt->fetchAll();
  71. }
  72. $stmt->closeCursor();
  73. //--------
  74. //
  75. // Create new Spreadsheet object
  76. $spreadsheet = new Spreadsheet();
  77. // Set document properties
  78. $spreadsheet->getProperties()->setCreator('Ingeniería La Salle')
  79. ->setLastModifiedBy('Ingeniería La Salle')
  80. ->setTitle('Cambio de carrera')
  81. ->setDescription('Cambio de carrera de alumnos.');
  82. $headerStyle = new Style();
  83. $headerStyle->applyFromArray(
  84. [
  85. 'fill' => [
  86. 'fillType' => Fill::FILL_SOLID,
  87. 'color' => ['argb' => 'FF001d68'],
  88. ],
  89. 'borders' => [
  90. 'bottom' => ['borderStyle' => Border::BORDER_THIN],
  91. 'right' => ['borderStyle' => Border::BORDER_MEDIUM],
  92. ],
  93. 'font' => [
  94. 'bold' => true,
  95. 'color' => ['argb' => 'FFFFFFFF'],
  96. ]
  97. ]
  98. );
  99. //--------
  100. $sheet = 0;
  101. // Rename worksheet
  102. $spreadsheet->setActiveSheetIndex($sheet);
  103. $spreadsheet->getActiveSheet()->setTitle('Programas');
  104. $row = 1;
  105. $col = 1;
  106. $spreadsheet->getActiveSheet()
  107. ->setCellValueByColumnAndRow($col++, 1, "Alumno")
  108. ->setCellValueByColumnAndRow($col++, 1, "Programa origen")
  109. ->setCellValueByColumnAndRow($col++, 1, "Clave origen")
  110. ->setCellValueByColumnAndRow($col++, 1, "Programa destino")
  111. ->setCellValueByColumnAndRow($col++, 1, "Clave destino")
  112. ->setCellValueByColumnAndRow($col++, 1, "Fecha de cambio");
  113. $colMax = $col;
  114. $colLetter = numberToLetter($colMax-1);
  115. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A1:'.$colLetter.'1');
  116. $row = 2;
  117. foreach($cambio_rs as $cambio){
  118. $col = 1;
  119. $spreadsheet->setActiveSheetIndex($sheet)
  120. ->setCellValueByColumnAndRow($col++, $row, $cambio["Usuario_apellidos"]." ".$cambio["Usuario_nombre"])
  121. ->setCellValueByColumnAndRow($col++, $row, $cambio["Carrera_desc_origen"]." ".$cambio["PlanEstudio_desc_origen"])
  122. ->setCellValueByColumnAndRow($col++, $row, $cambio["Usuario_claveULSA_origen"])
  123. ->setCellValueByColumnAndRow($col++, $row, $cambio["Carrera_desc_destino"]." ".$cambio["PlanEstudio_desc_destino"])
  124. ->setCellValueByColumnAndRow($col++, $row, $cambio["Usuario_claveULSA_destino"])
  125. ->setCellValueByColumnAndRow($col, $row, $cambio["CambioCarrera_fecha"]);
  126. $colLetter = numberToLetter($col);
  127. $spreadsheet->getActiveSheet()->getStyle($colLetter.$row)->getNumberFormat()
  128. ->setFormatCode('dd/mm/yyyy');
  129. $row++;
  130. }
  131. //Auto ajustar anchos
  132. for($col = 1; $col < $colMax; $col++)
  133. $spreadsheet->getActiveSheet()->getColumnDimension(numberToLetter($col))->setAutoSize(true);
  134. //$spreadsheet->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
  135. $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
  136. //--------
  137. $sheet = 1;
  138. // Rename worksheet
  139. $worksheet1 = $spreadsheet->createSheet();
  140. $worksheet1->setTitle('Procedencia');
  141. $spreadsheet->setActiveSheetIndex($sheet);
  142. $row = 1;
  143. $col = 1;
  144. $spreadsheet->getActiveSheet()
  145. ->setCellValueByColumnAndRow($col++, 1, "Alumno")
  146. ->setCellValueByColumnAndRow($col++, 1, "Institución origen")
  147. ->setCellValueByColumnAndRow($col++, 1, "Programa destino")
  148. ->setCellValueByColumnAndRow($col++, 1, "Clave destino")
  149. ->setCellValueByColumnAndRow($col++, 1, "Fecha de ingreso");
  150. $colMax = $col;
  151. $colLetter = numberToLetter($colMax-1);
  152. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A1:'.$colLetter.'1');
  153. $row = 2;
  154. foreach($procedencia_rs as $cambio){
  155. $col = 1;
  156. $spreadsheet->setActiveSheetIndex($sheet)
  157. ->setCellValueByColumnAndRow($col++, $row, $cambio["Usuario_apellidos"]." ".$cambio["Usuario_nombre"])
  158. ->setCellValueByColumnAndRow($col++, $row, $cambio["Alumno_procedencia"])
  159. ->setCellValueByColumnAndRow($col++, $row, $cambio["Carrera_desc"]." ".$cambio["PlanEstudio_desc"])
  160. ->setCellValueByColumnAndRow($col++, $row, $cambio["Usuario_claveULSA"])
  161. ->setCellValueByColumnAndRow($col, $row, $cambio["Alumno_fecha_ingreso"]);
  162. $colLetter = numberToLetter($col);
  163. $spreadsheet->getActiveSheet()->getStyle($colLetter.$row)->getNumberFormat()
  164. ->setFormatCode('dd/mm/yyyy');
  165. $row++;
  166. }
  167. //Auto ajustar anchos
  168. for($col = 1; $col < $colMax; $col++)
  169. $spreadsheet->getActiveSheet()->getColumnDimension(numberToLetter($col))->setAutoSize(true);
  170. //$spreadsheet->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
  171. $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
  172. //---------
  173. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  174. $spreadsheet->setActiveSheetIndex(0);
  175. // Redirect output to a client’s web browser (Xlsx)
  176. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  177. header('Content-Disposition: attachment;filename="cambiosCarrera.xlsx"');
  178. header('Cache-Control: max-age=0');
  179. // If you're serving to IE 9, then the following may be needed
  180. header('Cache-Control: max-age=1');
  181. // If you're serving to IE over SSL, then the following may be needed
  182. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  183. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  184. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  185. header('Pragma: public'); // HTTP/1.0
  186. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  187. $writer->save('php://output');
  188. exit;