Help, Error when opening my Excel in the cloud - CodeIgniter

0

Hi, I'm working on an excel report but when I run in my place it can be downloaded and opened normally without any problem, so I upload it to the cloud and I get this.

this is my code thanks

public function reporte_excel_total($lista) {

    set_time_limit(0);
    setlocale(LC_ALL, 'es_ES');
    $this->load->library('excel');
    $hoja = $this->excel->getActiveSheet();
    $this->excel->setActiveSheetIndex(0);
    $hoja->setTitle('REPORTE');
    $hoja->setCellValue('A1', 'REPORTE SEMÁFORO ESCUELA  TOTAL');
    $hoja->getStyle('A1')->getFont()->setSize(24);
    $hoja->getStyle('A1')->getFont()->setBold(true);
    $hoja->mergeCells('A1:M1');
    $hoja->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


    $hoja->mergeCells("A2:A3");
    $hoja->mergeCells("B2:B3");
    $hoja->mergeCells("C2:C3");
    $hoja->mergeCells("D2:D3");
    $hoja->mergeCells("E2:E3");
    $hoja->mergeCells("F2:F3");
    $hoja->mergeCells("G2:G3");
    $hoja->mergeCells("H2:H3");
    $hoja->mergeCells("I2:I3");

    $hoja->setCellValue('A2', "COD. MODULAR");
    $hoja->setCellValue('B2', "I.E.");
    $hoja->setCellValue('C2', "DIRECTOR");
    $hoja->setCellValue('D2', "RED");
    $hoja->setCellValue('E2', "DISTRITO");
    $hoja->setCellValue('F2', "NIVEL");
    $hoja->setCellValue('G2', "FECHA REGISTRO");
    $hoja->setCellValue('H2', "FECHA");
    $hoja->setCellValue('I2', "TURNO");

    $hoja->mergeCells("J2:M2");
    $hoja->setCellValue('J2', "DOCENTES")->getStyle('J2')->getFont()->setSize(15)->setBold(true);
    $hoja->getStyle('J2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $hoja->setCellValue('J3', "TOTAL DOCENTES");
    $hoja->setCellValue('K3', "ASISTENTES");
    $hoja->setCellValue('J3', "FALTANTES");
    $hoja->setCellValue('M3', "%");

    $hoja->mergeCells("N2:Q2");
    $hoja->setCellValue('N2', "ALUMNOS")->getStyle('N2')->getFont()->setSize(15)->setBold(true);
    $hoja->getStyle('N2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $hoja->setCellValue('N3', "TOTAL ALUMNOS");
    $hoja->setCellValue('O3', "ASISTENTES");
    $hoja->setCellValue('P3', "FALTANTES");
    $hoja->setCellValue('Q3', "%");

    $hoja->setAutoFilter('A3:Q3');

    $hoja->getStyle('A2:Q3')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('BDD7EE');
    $hoja->getColumnDimension('A')->setAutoSize(true);
    $hoja->getColumnDimension('B')->setAutoSize(true);
    $hoja->getColumnDimension('C')->setAutoSize(true);
    $hoja->getColumnDimension('D')->setAutoSize(true);
    $hoja->getColumnDimension('E')->setAutoSize(true);
    $hoja->getColumnDimension('F')->setAutoSize(true);
    $hoja->getColumnDimension('G')->setAutoSize(true);
    $hoja->getColumnDimension('H')->setAutoSize(true);
    $hoja->getColumnDimension('I')->setAutoSize(true);
    $hoja->getColumnDimension('J')->setAutoSize(true);
    $hoja->getColumnDimension('K')->setAutoSize(true);
    $hoja->getColumnDimension('L')->setAutoSize(true);
    $hoja->getColumnDimension('M')->setAutoSize(true);
    $hoja->getColumnDimension('N')->setAutoSize(true);
    $hoja->getColumnDimension('O')->setAutoSize(true);
    $hoja->getColumnDimension('P')->setAutoSize(true);
    $hoja->getColumnDimension('Q')->setAutoSize(true);

    $cont = 4;
    foreach ($lista as $obj) {

        $porc_docente = null;
        $porc_alumno = null;

        if ($obj->opcion_1 > 0) {
            $porc_docente = round(($obj->opcion_2 / $obj->opcion_1) * 100, 2);
            $this->color_celda($porc_docente, 'K', 'K', $cont, $hoja);
        }

        if ($obj->opcion_3 > 0) {
            $porc_alumno = round(($obj->opcion_4 / $obj->opcion_3) * 100, 2);
            $this->color_celda($porc_alumno, 'O', 'O', $cont, $hoja);
        }

        $hoja->setCellValueExplicit('A' . $cont, $obj->codigoModular, PHPExcel_Cell_DataType::TYPE_STRING);
        $hoja->getStyle('B' . $cont)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $hoja->setCellValueExplicit('B' . $cont, $obj->ie, PHPExcel_Cell_DataType::TYPE_STRING);
        $hoja->setCellValue('C' . $cont, $obj->director);
        $hoja->setCellValue('D' . $cont, $obj->red);
        $hoja->setCellValue('E' . $cont, $obj->distrito);
        $hoja->setCellValue('F' . $cont, $obj->nivel);
        $hoja->setCellValue('G' . $cont, $obj->fechaRegistro);
        $hoja->setCellValue('H' . $cont, $obj->fecha);
        $hoja->setCellValue('I' . $cont, $obj->turno);
        $hoja->setCellValue('J' . $cont, $obj->opcion_1);
        $hoja->setCellValue('K' . $cont, $obj->opcion_2);
        $hoja->setCellValue('L' . $cont, $obj->docentes_faltantes);
        $hoja->setCellValue('M' . $cont, $porc_docente);
        $hoja->setCellValue('N' . $cont, $obj->opcion_3);
        $hoja->setCellValue('O' . $cont, $obj->opcion_4);
        $hoja->setCellValue('P' . $cont, $obj->alumnos_faltantes);
        $hoja->setCellValue('Q' . $cont, $porc_alumno);


        $cont ++;
    }

    $filename = 'REPORTE_SEMÁFORO_ESCUELA_TOTAL.xls';

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="' . $filename . '"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');

    $objWriter->save('php://output');
}
    
asked by Ivan More Flores 05.05.2017 в 21:27
source

2 answers

0

Here I leave you as it is for the generation of xlsx I hope you find it useful. The other thing you can see is the type of charset that you are using, n may be different from the database to the script .

public function reporte_excel_total($lista) {

set_time_limit(0);
setlocale(LC_ALL, 'es_ES');
$this->load->library('excel');
$hoja = $this->excel->getActiveSheet();
$this->excel->setActiveSheetIndex(0);
$hoja->setTitle('REPORTE');
$hoja->setCellValue('A1', 'REPORTE SEMÁFORO ESCUELA  TOTAL');
$hoja->getStyle('A1')->getFont()->setSize(24);
$hoja->getStyle('A1')->getFont()->setBold(true);
$hoja->mergeCells('A1:M1');
$hoja->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


$hoja->mergeCells("A2:A3");
$hoja->mergeCells("B2:B3");
$hoja->mergeCells("C2:C3");
$hoja->mergeCells("D2:D3");
$hoja->mergeCells("E2:E3");
$hoja->mergeCells("F2:F3");
$hoja->mergeCells("G2:G3");
$hoja->mergeCells("H2:H3");
$hoja->mergeCells("I2:I3");

$hoja->setCellValue('A2', "COD. MODULAR");
$hoja->setCellValue('B2', "I.E.");
$hoja->setCellValue('C2', "DIRECTOR");
$hoja->setCellValue('D2', "RED");
$hoja->setCellValue('E2', "DISTRITO");
$hoja->setCellValue('F2', "NIVEL");
$hoja->setCellValue('G2', "FECHA REGISTRO");
$hoja->setCellValue('H2', "FECHA");
$hoja->setCellValue('I2', "TURNO");

$hoja->mergeCells("J2:M2");
$hoja->setCellValue('J2', "DOCENTES")->getStyle('J2')->getFont()->setSize(15)->setBold(true);
$hoja->getStyle('J2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$hoja->setCellValue('J3', "TOTAL DOCENTES");
$hoja->setCellValue('K3', "ASISTENTES");
$hoja->setCellValue('J3', "FALTANTES");
$hoja->setCellValue('M3', "%");

$hoja->mergeCells("N2:Q2");
$hoja->setCellValue('N2', "ALUMNOS")->getStyle('N2')->getFont()->setSize(15)->setBold(true);
$hoja->getStyle('N2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$hoja->setCellValue('N3', "TOTAL ALUMNOS");
$hoja->setCellValue('O3', "ASISTENTES");
$hoja->setCellValue('P3', "FALTANTES");
$hoja->setCellValue('Q3', "%");

$hoja->setAutoFilter('A3:Q3');

$hoja->getStyle('A2:Q3')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('BDD7EE');
$hoja->getColumnDimension('A')->setAutoSize(true);
$hoja->getColumnDimension('B')->setAutoSize(true);
$hoja->getColumnDimension('C')->setAutoSize(true);
$hoja->getColumnDimension('D')->setAutoSize(true);
$hoja->getColumnDimension('E')->setAutoSize(true);
$hoja->getColumnDimension('F')->setAutoSize(true);
$hoja->getColumnDimension('G')->setAutoSize(true);
$hoja->getColumnDimension('H')->setAutoSize(true);
$hoja->getColumnDimension('I')->setAutoSize(true);
$hoja->getColumnDimension('J')->setAutoSize(true);
$hoja->getColumnDimension('K')->setAutoSize(true);
$hoja->getColumnDimension('L')->setAutoSize(true);
$hoja->getColumnDimension('M')->setAutoSize(true);
$hoja->getColumnDimension('N')->setAutoSize(true);
$hoja->getColumnDimension('O')->setAutoSize(true);
$hoja->getColumnDimension('P')->setAutoSize(true);
$hoja->getColumnDimension('Q')->setAutoSize(true);

$cont = 4;
foreach ($lista as $obj) {

    $porc_docente = null;
    $porc_alumno = null;

    if ($obj->opcion_1 > 0) {
        $porc_docente = round(($obj->opcion_2 / $obj->opcion_1) * 100, 2);
        $this->color_celda($porc_docente, 'K', 'K', $cont, $hoja);
    }

    if ($obj->opcion_3 > 0) {
        $porc_alumno = round(($obj->opcion_4 / $obj->opcion_3) * 100, 2);
        $this->color_celda($porc_alumno, 'O', 'O', $cont, $hoja);
    }

    $hoja->setCellValueExplicit('A' . $cont, $obj->codigoModular, PHPExcel_Cell_DataType::TYPE_STRING);
    $hoja->getStyle('B' . $cont)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
    $hoja->setCellValueExplicit('B' . $cont, $obj->ie, PHPExcel_Cell_DataType::TYPE_STRING);
    $hoja->setCellValue('C' . $cont, $obj->director);
    $hoja->setCellValue('D' . $cont, $obj->red);
    $hoja->setCellValue('E' . $cont, $obj->distrito);
    $hoja->setCellValue('F' . $cont, $obj->nivel);
    $hoja->setCellValue('G' . $cont, $obj->fechaRegistro);
    $hoja->setCellValue('H' . $cont, $obj->fecha);
    $hoja->setCellValue('I' . $cont, $obj->turno);
    $hoja->setCellValue('J' . $cont, $obj->opcion_1);
    $hoja->setCellValue('K' . $cont, $obj->opcion_2);
    $hoja->setCellValue('L' . $cont, $obj->docentes_faltantes);
    $hoja->setCellValue('M' . $cont, $porc_docente);
    $hoja->setCellValue('N' . $cont, $obj->opcion_3);
    $hoja->setCellValue('O' . $cont, $obj->opcion_4);
    $hoja->setCellValue('P' . $cont, $obj->alumnos_faltantes);
    $hoja->setCellValue('Q' . $cont, $porc_alumno);


    $cont ++;
}

    // Renombrar Hoja
    $this->excel->getActiveSheet()->setTitle('Reporte');

    // Establecer la hoja activa, para que cuando se abra el documento se muestre primero.
    $this->excel->setActiveSheetIndex(0);

    // Se modifican los encabezados del HTTP para indicar que se envía un archivo de Excel.
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="reporte.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
    $objWriter->save('php://output');
    exit;   

}

    
answered by 08.05.2017 в 16:50
0

It is likely that in production there is a change that is generating a warning message in the browser, in which case you should dump the message somewhere to consult it:

public function reporte_excel_total($lista) {
    /* A partir de ahora cualquier salida al navegador se guardará en un buffer */
    ob_start();
    /* Obtenemos el listado de locales disponibles en el sistema */
    file_put_contents('log.txt', shell_exec('locale -a'), FILE_APPEND);

    set_time_limit(0);
    /* Damos más opciones, en el log aparecerán los disponibles */
    setlocale(LC_ALL, 'es_ES', 'es');
    $this->load->library('excel');


    /*****
      EL RESTO DE TU CÓDIGO
      (lo omito por claridad)
    *****/


    $filename = 'REPORTE_SEMÁFORO_ESCUELA_TOTAL.xls';

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="' . $filename . '"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');

    /* Obtenemos el contenido del buffer y lo guardamos */
    file_put_contents('log.txt', ob_get_contents(), FILE_APPEND);
    /* Borramos el contenido que iba a ser enviado al navegador antes que el excel */
    ob_end_clean();

    $objWriter->save('php://output');
}

What I do is:

  • I execute a locales -a to dump the file log.txt of the available locales. NOTE: It only works on Linux machines (VPS servers "in the cloud" are usually Linux).
  • I retain any warning message generated in a buffer without sending to the browser.
  • I add all retained information to the log.txt file for later review and debugging.
  • Finally, clean any previous message by ending buffering so that Excel is generated as usual.

When you check if the excel is correctly generated, check the contents of the log.txt file that will have been generated on your server. If it has not been generated it has probably been because you need to give write permissions to the directory.

    
answered by 09.05.2017 в 12:54