Problems generating an excel with phpexcel

0

I am exporting a MYSQL database to an Excel file using the phpexcel library in PHP.

    require_once('phpexcel/Classes/PHPExcel.php');
require '../database.php';
$pdo = Database::Conectar();

        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
        date_default_timezone_set('Europe/London');



    $objPHPExcel = new PHPExcel();/*SE CREA OBJETO DE PHPExcel*/

    $objPHPExcel->getProperties()->setCreator("Developero")/*CREADOR DEL REPORTE*/
           ->setLastModifiedBy("Maarten Balliauw")
           ->setTitle("Office 2007 XLSX Test Document")
           ->setSubject("Office 2007 XLSX Test Document")
           ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
           ->setKeywords("office 2007 openxml php")
           ->setCategory("Test result file");

    /*AGREGAR TITULOS DE LA TABLA DE EXCEL*/
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '#')
        ->setCellValue('B1', 'Respuesta 1')
        ->setCellValue('C1', 'Respuesta 2')
        ->setCellValue('D1', 'Respuesta 3');

    //$informe = $this->model->MostrarCargo();
    $sql = "SELECT * FROM respuesta";
    $stm = $pdo->prepare($sql);
    $stm->execute();


    $i = 2;/*SE INICIALIZA EN 2 PARA QUE COMIENCE EN LA FILA 2 */
    /*RELLENO DE LA TABLA*/
    foreach($informe as $row):
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue("A$i", $row->id)
                    ->setCellValue("B$i", $row->a)
                    ->setCellValue("C$i", $row->b)
                    ->setCellValue("D$i", $row->c);

        $i++;/*SE INCREMENTA LA FILA*/
    endforeach;

    /*TITULO DE LA HOJA EXCEL*/

    $objPHPExcel->getActiveSheet()->setTitle('Informe');

    /*HOJA DE INICIO*/
    $objPHPExcel->setActiveSheetIndex(0);

    /*SE CREA EL EXCEL*/

              // Redirect output to a client’s web browser (Excel5)
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="Informe.xlsx"');
      header('Cache-Control: max-age=0');
      // If you're serving to IE 9, then the following may be needed
      header('Cache-Control: max-age=1');

      // If you're serving to IE over SSL, then the following may be needed
      header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
      header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
      header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
      header ('Pragma: public'); // HTTP/1.0

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');/*SE CREA EL EXCEL*/
    $objWriter->save('php://output');/*SE GUARDA EL EXCEL*/
    exit;

When I remove the following lines:

//$informe = $this->model->MostrarCargo();
$sql = "SELECT * FROM respuesta";
$stm = $pdo->prepare($sql);
$stm->execute();


$i = 2;/*SE INICIALIZA EN 2 PARA QUE COMIENCE EN LA FILA 2 */
/*RELLENO DE LA TABLA*/
foreach($informe as $row):
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue("A$i", $row->id)
                ->setCellValue("B$i", $row->a)
                ->setCellValue("C$i", $row->b)
                ->setCellValue("D$i", $row->c);

    $i++;/*SE INCREMENTA LA FILA*/
endforeach;

The excel file recognizes the headers. When I add the code, the excel does not work for me. Throw the following message: "Excel can not open the file 'report.xlsx because the format or the extension of this are not valid."

    
asked by Carlos Cespedes 30.10.2018 в 21:09
source

0 answers