Error exporting database to Excel with PHPExcel library

1

I am trying to export a table from my database to an excel file, using the library PHPExcel

The structure of my table is:

  

reg, folio, key, date, place, address, telephone, name, subject, depto1, depto2, state, observations, scan, evidence

and my code PHP to export is:

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

 $conexion = mysqli_connect ("localhost", "xxxxx", "xxxxxx");
 mysqli_select_db ( $conexion, "xxxxxx");

 $sql = "SELECT * FROM solicitudes ORDER BY folio DESC";
 $resultado = mysqli_query ($conexion,$sql) or die (mysqli_error ());
 $registros = mysqli_num_rows ($resultado);

 if ($registros > 0) {
   require_once 'PHPExcel/Classes/PHPExcel.php';
   $objPHPExcel = new PHPExcel();

   //Informacion del excel
   $objPHPExcel->
    getProperties()
        ->setCreator("lahuerta")
        ->setLastModifiedBy("lahuerta")
        ->setTitle("Exportar Base de Datos")
        ->setSubject("Tabla")
        ->setDescription("Documento generado con PHPExcel")
        ->setKeywords("lahuerta  con  phpexcel")
        ->setCategory("solicitudes");    

   $i = 1;    
   while ($registro = mysqli_fetch_object ($resultado)) {

      $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $registro->name);

      $i++;

   }
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="tabla.xlsx"');
header('Cache-Control: max-age=0');

$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');
exit;
mysql_close ();
?>

I am receiving the following error several times:

  

Notice: Undefined property: stdClass :: $ name in /home/exportar.php on line 32

I also receive these 3 errors after the aforementioned:

  

Warning: Can not modify header information - headers already sent by (output started at /home/exportar.php:32) in /home/exportar.php on line 38

     

Warning: Can not modify header information - headers already sent by (output started at /home/exportar.php:32) in /home/exportar.php on line 39

     

Warning: Can not modify header information - headers already sent by (output started at /home/exportar.php:32) in /home/exportar.php on line 40

and when I finish the list of errors it shows me some strange encodings:

  

PKa JG D X [Content_Types] .xml MN 0 " % nY vAa (0 ؖ g w {& i @ nbE { y d8l m X ( ) F ; @ 1_ c) j x /% E y QĿi ! K y3 J < Z1 0? Y L% zV c Ib7 a / l5P1: q r j j0A u "" ( W M ) Tj ({ܲ > O , X * > B ~ ׭ Ӥ6 J = oBZ t X4 Cg , QgmrL ٯ c e t Z ? hPv ± u j R } Cv PKa J 78 K_rels / .rels j 0 { { 1F ^ ʠ 2 l $ -}

The version of PHP is the 5.6 and the extension Zip I have it enabled.

    
asked by Sharly Infinitywars 21.07.2017 в 21:28
source

1 answer

1

The first problem ( eg: Notice: Undefined property... ) is because $registro->name is not defined. This can be validated when viewing structure of table solicitudes .

Then the Warning: Cannot modify header information... appear because the Notice: Undefined property... and the method header() must be called:

  

Before displaying anything on the screen, HTML tags, blank lines from a file or from PHP. It is a common error to read code with functions such as include or require, or other types of file access functions that include spaces or blank lines that are displayed before calling the header () function. The same problem occurs when a single PHP / HTML file is used.

Solution:

The solution should be simply to modify the line 32 .

Assuming that the value you want to add to the cell is the nombre in the solicitud , then you should write $registro->nombre .

Example:

// "nombre" es uno de los campos de la tabla "solicitudes"
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$i, $registro->nombre);

-

PD : At the end of your script you are using mysql_close() , when the connection has been using mysqli_connect , that is, you should use mysqli_close($conexion) . Anyway this does not cause problems because it is just after the exit , so it never gets executed.

    
answered by 21.07.2017 / 21:54
source