Guest User

Untitled

a guest
May 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. <?php
  2. $conexion = new mysqli('localhost','root','admin123','database',3306);
  3. if (mysqli_connect_errno()) {
  4. printf("La conexión con el servidor de base de datos falló: %sn", mysqli_connect_error());
  5. exit();
  6. }
  7.  
  8. $consulta = "SELECT servicio_No, hora, tipo_Ambulancia, tipo_Traslado, horario, cliente, fecha_Solicitud, hora1, hor, regulador FROM servicio ORDER BY servicio_No";
  9. $resultado = $conexion->query($consulta);
  10. if($resultado->num_rows > 0 ){
  11.  
  12. date_default_timezone_set('America/Bogota');
  13.  
  14. if (PHP_SAPI == 'cli')
  15. die('Este archivo solo se puede ver desde un navegador web');
  16.  
  17. /** Se agrega la libreria PHPExcel */
  18. require_once 'Classes/PHPExcel.php';
  19.  
  20. // Se crea el objeto PHPExcel
  21. $objPHPExcel = new PHPExcel();
  22.  
  23. // Se asignan las propiedades del libro
  24. $objPHPExcel->getProperties()->setCreator("Trujii") //Autor
  25. ->setLastModifiedBy(";D") //Ultimo usuario que lo modificó
  26. ->setTitle("Reporte Excel con PHP y MySQL")
  27. ->setSubject("Reporte Excel con PHP y MySQL")
  28. ->setDescription("Reporte de servicios")
  29. ->setKeywords("reporte servicio")
  30. ->setCategory("Reporte excel");
  31.  
  32. $tituloReporte = "Relación de servicios";
  33. $titulosColumnas = array('SERVICIO NO', 'HORA', 'TIPO AMBULANCIA', 'TIPO TRASLADO', 'HORARIO', 'CLIENTE', 'FECHA SOLICITUD', 'HORA', 'HOR', 'REGULADOR');
  34.  
  35. $objPHPExcel->setActiveSheetIndex(0)
  36. ->mergeCells('A1:J1');
  37.  
  38. // Se agregan los titulos del reporte
  39. $objPHPExcel->setActiveSheetIndex(0)
  40. ->setCellValue('A1',$tituloReporte)
  41. ->setCellValue('A3', $titulosColumnas[0])
  42. ->setCellValue('B3', $titulosColumnas[1])
  43. ->setCellValue('C3', $titulosColumnas[2])
  44. ->setCellValue('D3', $titulosColumnas[3])
  45. ->setCellValue('E3', $titulosColumnas[4])
  46. ->setCellValue('F3', $titulosColumnas[5])
  47. ->setCellValue('G3', $titulosColumnas[6])
  48. ->setCellValue('H3', $titulosColumnas[7])
  49. ->setCellValue('I3', $titulosColumnas[8])
  50. ->setCellValue('J3', $titulosColumnas[9])
  51. ->setCellValue('K3', $titulosColumnas[10]));
  52.  
  53. //Se agregan los datos de los alumnos
  54. $i = 4;
  55. while ($fila = $resultado->fetch_array()) {
  56. $objPHPExcel->setActiveSheetIndex(0)
  57. ->setCellValue('A'.$i, $fila['servicio_No'])
  58. ->setCellValue('B'.$i, $fila['hora'])
  59. ->setCellValue('C'.$i, $fila['tipo_Ambulancia'])
  60. ->setCellValue('D'.$i, $fila['tipo_Traslado'])
  61. ->setCellValue('E'.$i, $fila['horario'])
  62. ->setCellValue('F'.$i, $fila['cliente'])
  63. ->setCellValue('G'.$i, $fila['fecha_Solicitud'])
  64. ->setCellValue('H'.$i, $fila['hora1'])
  65. ->setCellValue('I'.$i, $fila['hor'])
  66. ->setCellValue('J'.$i, $fila['regulador']));
  67. $i++;
  68. }
  69.  
  70. $estiloTituloReporte = array(
  71. 'font' => array(
  72. 'name' => 'Verdana',
  73. 'bold' => true,
  74. 'italic' => false,
  75. 'strike' => false,
  76. 'size' =>20,
  77. 'color' => array(
  78. 'rgb' => 'FFFFFF'
  79. )
  80. ),
  81. 'fill' => array(
  82. 'type' => PHPExcel_Style_Fill::FILL_SOLID,
  83. 'color' => array('argb' => 'FF220835')
  84. ),
  85. 'borders' => array(
  86. 'allborders' => array(
  87. 'style' => PHPExcel_Style_Border::BORDER_NONE
  88. )
  89. ),
  90. 'alignment' => array(
  91. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  92. 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  93. 'rotation' => 0,
  94. 'wrap' => TRUE
  95. )
  96. );
  97.  
  98. $estiloTituloColumnas = array(
  99. 'font' => array(
  100. 'name' => 'Arial',
  101. 'bold' => true,
  102. 'color' => array(
  103. 'rgb' => 'FFFFFF'
  104. )
  105. ),
  106. 'fill' => array(
  107. 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  108. 'rotation' => 90,
  109. 'startcolor' => array(
  110. 'rgb' => '00abdd'
  111. ),
  112. 'endcolor' => array(
  113. 'argb' => '00abdd'
  114. )
  115. ),
  116. 'borders' => array(
  117. 'top' => array(
  118. 'style' => PHPExcel_Style_Border::BORDER_MEDIUM ,
  119. 'color' => array(
  120. 'rgb' => '143860'
  121. )
  122. ),
  123. 'bottom' => array(
  124. 'style' => PHPExcel_Style_Border::BORDER_MEDIUM ,
  125. 'color' => array(
  126. 'rgb' => '143860'
  127. )
  128. )
  129. ),
  130. 'alignment' => array(
  131. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  132. 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  133. 'wrap' => TRUE
  134. ));
  135.  
  136. $estiloInformacion = new PHPExcel_Style();
  137. $estiloInformacion->applyFromArray(
  138. array(
  139. 'font' => array(
  140. 'name' => 'Arial',
  141. 'color' => array(
  142. 'rgb' => '000000'
  143. )
  144. ),
  145. 'fill' => array(
  146. 'type' => PHPExcel_Style_Fill::FILL_SOLID,
  147. 'color' => array('argb' => '00abdd')
  148. ),
  149. 'borders' => array(
  150. 'left' => array(
  151. 'style' => PHPExcel_Style_Border::BORDER_THIN ,
  152. 'color' => array(
  153. 'rgb' => '3a2a47'
  154. )
  155. )
  156. )
  157. ));
  158.  
  159. $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->applyFromArray($estiloTituloReporte);
  160. $objPHPExcel->getActiveSheet()->getStyle('A3:J3')->applyFromArray($estiloTituloColumnas);
  161. $objPHPExcel->getActiveSheet()->setSharedStyle($estiloInformacion, "A4:BD".($i-1));
  162.  
  163. for($i = 'A'; $i <= 'J'; $i++){
  164. $objPHPExcel->setActiveSheetIndex(0)
  165. ->getColumnDimension($i)->setAutoSize(TRUE);
  166. }
  167.  
  168. // Se asigna el nombre a la hoja
  169. $objPHPExcel->getActiveSheet()->setTitle('servicio');
  170.  
  171. // Se activa la hoja para que sea la que se muestre cuando el archivo se abre
  172. $objPHPExcel->setActiveSheetIndex(0);
  173. // Inmovilizar paneles
  174. //$objPHPExcel->getActiveSheet(0)->freezePane('A4');
  175. $objPHPExcel->getActiveSheet(0)->freezePaneByColumnAndRow(0,10);
  176.  
  177. // Se manda el archivo al navegador web, con el nombre que se indica (Excel2007)
  178. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  179. header('Content-Disposition: attachment;filename="ReportedeSERVICIOS.xlsx"');
  180. header('Cache-Control: max-age=0');
  181.  
  182. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  183. $objWriter->save('php://output');
  184. exit;
  185.  
  186. }
  187. else{
  188. print_r('No hay resultados para mostrar');
  189. }
  190. ?>
Add Comment
Please, Sign In to add comment