Problème avec la fonction header()
Résolu
beaf
Messages postés
292
Statut
Membre
-
Alain_42 Messages postés 5413 Statut Membre -
Alain_42 Messages postés 5413 Statut Membre -
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
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
A voir également:
- Problème avec la fonction header()
- Fonction si et - Guide
- Fonction miroir - Guide
- Fonction moyenne excel - Guide
- Fonction remplacer sur word - Guide
- 400 bad request request header or cookie too large ✓ - Forum Réseaux sociaux
5 réponses
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
<?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
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
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
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
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>';
?>