Report between 2 dates in Codeigniter

11

Excuse me; I get this error when exporting to Excel. Can you tell me why it would be the error?

In this part I put the function I did to export to Excel:

public function imprimir_excel($FInicial, $FFinal){

    //cargando la libreria
    $this->load->library('PHPExcel');

    // Propiedades del archivo excel

    // hacemos que queda activa la primera hoja al abrir el excel
    $this->phpexcel->setActiveSheetIndex(0);

    // Hoja de excel para trabajar con PHP

    $sheet = $this->phpexcel->getActiveSheet();

    //aplicamos dimensiones a las celdas dependiendo del contenido
    $sheet->getColumnDimension('A')->setWidth(6);
    $sheet->getColumnDimension('C')->setWidth(12);
    $sheet->getColumnDimension('B')->setWidth(25);
    $sheet->getColumnDimension('D')->setWidth(14);
    $sheet->getColumnDimension('E')->setWidth(12);

    //Excribimos el titulo de las columnas de nuestro reporte
    $sheet->setCellValue('A1', 'NUM.');
    $sheet->setCellValue('B1', 'NOMBRE');
    $sheet->setCellValue('C1', 'Distrito');
    $sheet->setCellValue('D1', 'fecha');
    $sheet->setCellValue('E1', 'Observacion');

    //consultamos nuestros reportes del registros  especifico
    $reportes=$this->model_reportes->reportesGenera($FInicial, $FFinal);

    $i=1;
    if (isset($reportes)) {

        foreach ($reportes as $fila):

            $idregistro=$fila->idregistro;
            $long=strlen($idregistro);
            $generar=str_repeat('0',4-$long);
            // $id_generado=$generar.$idregistro;

            ++$i;

            //una vez que tenemos los valores escribimos en nuestro archivo de excel
            // $sheet->setCellValue('A'.$i, $id_generado);
            $sheet->setCellValue('B'.$i, $fila->nombre_establecimiento);
            $sheet->setCellValue('C'.$i, $fila->distrito);
            $sheet->setCellValue('D'.$i, $fila->fecha);
            $sheet->setCellValue('E'.$i, $fila->observacion);

            //aplicamos un estilo a nuestra celdas de contenido
            // $this->phpexcel->getActiveSheet()->getStyle('A'.$i.':I'.$i)->applyFromArray($filas_formato);
        endforeach;
    }
    //aplicamos un estilo

    //$this->phpexcel->getActiveSheet()->getStyle('A1:I1')->applyFromArray($titulo_formato);

    // Preparando para generar archivo de excel
    header("Content-Type: application/vnd.ms-excel");
    $nombreArchivo = date('Y-m-d');
    header("Content-Disposition: attachment; filename=\"$nombreArchivo.xls\"");
    header("Cache-Control: max-age=0");

    // Genera Excel
    $writer = PHPExcel_IOFactory::createWriter($this->phpexcel, "Excel5");

    // descarga el archivo de Excel
    $writer->save('php://output');
    exit;

}
    
asked by Ronald Rios 23.07.2016 в 20:01
source

4 answers

1

Change your file name for that and modify the header, able to solve your problem:

$nombreArchivo = 'sample_' . time() . '.xls'; 
header('Content-Disposition: attachment;filename="' . $nombreArchivo . '"');
    
answered by 23.07.2016 в 20:50
0

Reviewing your code I can see two things:

  • $i=1; // should be $i=2; since this line would be the header and you already assigned it before. Something functional
  • It seems to me that you should still assign a name to it when saving. $writer->save($nombreArchivo.'.xls'); // Something technical

Try these things and write your comments

    
answered by 17.10.2016 в 15:37
0

At the beginning: header("Content-Type: text/html;charset=utf-8");

Final headers:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="nombreFichero.xlsx"');
header('Cache-Control: max-age=0');

Seen at: PHPExcel Cells With utf8

    
answered by 10.10.2018 в 21:50
0

This is the solution. Corresponds to give a correct name and remove the date.

header("Content-Type: application/vnd.ms-excel");
$nombreArchivo = "Nombre";
header("Content-Disposition: attachment; filename=\"$nombreArchivo.xls\"");
header("Cache-Control: max-age=0");
    
answered by 15.01.2019 в 19:44