Problème avec la fonction header()

Résolu
Bonjour,

j'essai d'exporter des données en Excel à partir de PHP, voici le message que je recois ::

Cannot modify header information - headers already sent by (output started at

Warning: Cannot modify header information - headers already sent by (output started at C:\Users\Armel\Desktop\tAr\pageEntete.php:142) in C:\Users\Armel\Desktop\tpAr\phpExcel4.php on line 15
Prenom Nom age Mary Johnson 25 Amanda Miller 18 James Brown 31 Patricia Williams 7 Michael Davis 43 Sarah Miller 24 Patrick Miller 27

5 réponses

  1. avant l'instruction header .. il ne doit pas y avoir de caractère html, ni d'espace
    1. Voici un exemple de mon code que j'ai pri dans un tuto:

      <?PHP
      $data = array(
      array("prenom" => "Mary", "nom" => "Johnson", "age" => 25),
      array("prenom" => "Amanda", "nom" => "Miller", "age" => 18),
      array("prenom" => "James", "nom" => "Brown", "age" => 31),
      array("prenom" => "Patricia", "nom" => "Williams", "age" => 7),
      array("prenom" => "Michael", "nom" => "Davis", "age" => 43),
      array("prenom" => "Sarah", "nom" => "Miller", "age" => 24),
      array("prenom" => "Patrick", "nom" => "Miller", "age" => 27)
      );
      ?>
      <?PHP
      header("Content-Type: text/plain");

      $flag = false;
      foreach($data as $row) {
      if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\r\n";
      $flag = true;
      }
      echo implode("\t", array_values($row)) . "\r\n";
      }
      exit;
      ?>

      Merci de votre aide
      1. quel editeur utilises tu ?

        si c'est notepad++, vas dans le menu:
        encodage

        et mets en Encodage uft8 sans BOM

        si tu as un autre editeur il doit y avoir une option semblable
        1. j'ai profondément modifié ton code pour génération d'un fichier excel à partir d'un script php:

          <?php
          	//pour excel les header sont ceux la
              // envoyer le résultat du script dans une feuille Excel
              header("Content-type: application/vnd.ms-excel");
              // nom au fichier Excel
          	$nom_fich_excel='le_nom_que_tu_veux'.'.xls';
             header("Content-Disposition: attachment; filename=".$nom_fich_excel);
             
             $data = array(
          array("prenom" => "Mary", "nom" => "Johnson", "age" => 25),
          array("prenom" => "Amanda", "nom" => "Miller", "age" => 18),
          array("prenom" => "James", "nom" => "Brown", "age" => 31),
          array("prenom" => "Patricia", "nom" => "Williams", "age" => 7),
          array("prenom" => "Michael", "nom" => "Davis", "age" => 43),
          array("prenom" => "Sarah", "nom" => "Miller", "age" => 24),
          array("prenom" => "Patrick", "nom" => "Miller", "age" => 27)
          );
          
          //il vaut mieux generer une table pour afficher dans excel
          echo '<table>';
          $flag = false;
          foreach($data as $row) {
          	echo '<tr>';
          	foreach ($row as $key=>$value){
          	if(!$flag) {
          		// generation titres des colonnes
          		echo '<th>'.$key.'</th>';
          		
          	}else{
          		//valeurs de chaque ligne dans les colonnes
          		echo '<td>'.$value.'</td>';
          	}
          	
          	}
          	//après la première boucle on passe le flag a true pour arreter l'affichage des titres de colonnes
          	$flag = true;
          	echo '</tr>';
          }
          echo '</table>';
          ?>