Código PHP:
<?php ini_set("memory_limit","3000M");
$base= "Consultas";
$db = mysql_connect("127.0.0.1", "root", "");
mysql_select_db($base,$db);
date_default_timezone_set("America/Bogota");
$fecha = date ("Y-m-d");
if( $_POST['usuario'] != '' ) $usuario=$_POST['usuario'];
else $usuario=$_GET['usuario'];
if( $_POST['fechaini'] != '' ) $fechaini=$_POST['fechaini'];
else $fechaini=$_GET['fechaini'];
if( $_POST['fechafin'] != '' ) $fechafin=$_POST['fechafin'];
else $fechafin=$_GET['fechafin'];
$nomArc = "reporte/".$usuario."-".$fecha.".xlsx";
if( !file_exists( $nomArc ) )
{
$sql = "select * from consultas.consultas
where usuario = '$usuario'
order by consecutivo desc";
$rs = mysql_query($sql,$db);
//$j = 0;
error_reporting(E_ALL);
ini_set('include_path', ini_get('include_path').';../Classes/');
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Paty");
$objPHPExcel->getProperties()->setLastModifiedBy("Paty");
while( $row = mysql_fetch_array( $rs ) )
{
$sql = "select * from consultas.consultas
where usuario = '$usuario' and date(fecha_solicitud) between '$fechaini' and '$fechafin'
order by consecutivo desc ";
$rs2 = mysql_query($sql,$db);
if( mysql_num_rows( $rs2 ) > 0 )
{
$j=0;
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex($j);
//$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'CAmp1');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'CAmp2');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'CAmp3');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'CAmp4');
$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'CAmp5');
$objPHPExcel->getActiveSheet()->SetCellValue('F1', 'CAmp6');
$objPHPExcel->getActiveSheet()->SetCellValue('G1', 'CAmp7');
$objPHPExcel->getActiveSheet()->SetCellValue('H1', 'CAmp8');
$objPHPExcel->getActiveSheet()->SetCellValue('I1', 'CAmp9');
$objPHPExcel->getActiveSheet()->SetCellValue('J1', 'CAmp10');
$objPHPExcel->getActiveSheet()->SetCellValue('K1', 'CAmp11');
$objPHPExcel->getActiveSheet()->SetCellValue('L1', 'CAmp12');
$objPHPExcel->getActiveSheet()->SetCellValue('M1', 'CAmp13');
$objPHPExcel->getActiveSheet()->SetCellValue('N1', 'CAmp14');
$objPHPExcel->getActiveSheet()->SetCellValue('O1', 'CAmp15');
$objPHPExcel->getActiveSheet()->SetCellValue('P1', 'CAmp16');
$objPHPExcel->getActiveSheet()->SetCellValue('Q1', 'CAmp17');
$objPHPExcel->getActiveSheet()->SetCellValue('R1', 'CAmp18');
$objPHPExcel->getActiveSheet()->SetCellValue('S1', 'CAmp19');
$objPHPExcel->getActiveSheet()->SetCellValue('T1', 'CAmp20');
$objPHPExcel->getActiveSheet()->SetCellValue('U1', 'CAmp21');
$objPHPExcel->getActiveSheet()->SetCellValue('V1', 'CAmp22');
$objPHPExcel->getActiveSheet()->SetCellValue('W1', 'CAmp23');
$objPHPExcel->getActiveSheet()->SetCellValue('X1', 'CAmp24');
$objPHPExcel->getActiveSheet()->SetCellValue('Y1', 'CAmp25');
$j++;
$i = 2;
while( $row2 = mysql_fetch_array( $rs2 ) )
{
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i, $row2[0]);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i, $row2[1]);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i, $row2[2]);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i, $row2[3]);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i, $row2[4]);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i, $row2[5]);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i, $row2[6]);
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i, $row2[7]);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i, $row2[8]);
$objPHPExcel->getActiveSheet()->SetCellValue('J'.$i, $row2[9]);
$objPHPExcel->getActiveSheet()->SetCellValue('K'.$i, $row2[10]);
$objPHPExcel->getActiveSheet()->SetCellValue('L'.$i, $row2[11]);
$objPHPExcel->getActiveSheet()->SetCellValue('M'.$i, $row2[12]);
$objPHPExcel->getActiveSheet()->SetCellValue('N'.$i, $row2[13]);
$objPHPExcel->getActiveSheet()->SetCellValue('O'.$i, $row2[14]);
$objPHPExcel->getActiveSheet()->SetCellValue('P'.$i, $row2[15]);
$objPHPExcel->getActiveSheet()->SetCellValue('Q'.$i, $row2[16]);
$objPHPExcel->getActiveSheet()->SetCellValue('R'.$i, $row2[17]);
$objPHPExcel->getActiveSheet()->SetCellValue('S'.$i, $row2[18]);
$objPHPExcel->getActiveSheet()->SetCellValue('T'.$i, $row2[19]);
$objPHPExcel->getActiveSheet()->SetCellValue('U'.$i, $row2[20]);
$objPHPExcel->getActiveSheet()->SetCellValue('V'.$i, $row2[21]);
$objPHPExcel->getActiveSheet()->SetCellValue('W'.$i, $row2[22]);
$objPHPExcel->getActiveSheet()->SetCellValue('X'.$i, $row2[23]);
$objPHPExcel->getActiveSheet()->SetCellValue('Y'.$i, $row2[24]);
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('Reporte');
}
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save( $nomArc ); //GUARDA EL ARCHIVO EN LA RUTA INDICADA EN CODIGO
}
elseif( file_exists( $nomArc ) ) echo "El reporte para ".$usuario.", ya fue realizado el dia de hoy. Por favor verifique.\n";
//OBLIGA A VENTANA GUARDAR
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$nomArc.'"'); //NOMBRE PARA LA OPCION GUARDAR
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');//PARA SALIR DIRECTAMENTE EN EL BROWSER