Modifier un code PHP pour l'adapter à PHPExcel

Résolu/Fermé
zerdg Messages postés 86 Date d'inscription jeudi 21 mai 2015 Statut Membre Dernière intervention 30 novembre 2017 - Modifié par zerdg le 27/05/2015 à 15:04
zerdg Messages postés 86 Date d'inscription jeudi 21 mai 2015 Statut Membre Dernière intervention 30 novembre 2017 - 28 mai 2015 à 12:48
Bonjour,
Étant en stage je cherche depuis quelques temps à afficher une requête SQL dans un fichier excel. J'ai trouvé un code qui marche mais je ne le comprend pas entièrement ainsi je souhaiterais l'adapter pour à PHPExcel.

le code :
<?php

$DB_Server = "localhost"; // MySQL Server
$DB_Username = "root"; // MySQL Username
$DB_Password = "root"; // MySQL Password
$DB_DBName = "miniEnqueteSalaries"; // MySQL Database Name
$DB_TBLName = "reponses"; // MySQL Table Name
$xls_filename = 'export_'.date('Y-m-d').'.xls'; // Define Excel (.xls) file name


// Create MySQL connection
$sql = 'select * from reponses';
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Failed to connect to MySQL:<br />" . mysql_error() . "<br />" . mysql_errno());
// Select database
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Failed to select database:<br />" . mysql_error(). "<br />" . mysql_errno());
// Execute query
$result = @mysql_query($sql,$Connect) or die("Failed to execute query:<br />" . mysql_error(). "<br />" . mysql_errno());

// Header info settings
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");
header('Content-Type: text/html; charset=utf-6');

/***** Start of Formatting for Excel *****/
// Define separator (defines columns in excel & tabs in word)
$sep = "\t"; // tabbed character

// Start of printing column names as names of MySQL fields
for ($i = 0; $i<mysql_num_fields($result); $i++) {
echo mysql_field_name($result, $i) . "\t";
}
print("\n");
// End of printing column names

// Start while loop to get data
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result); $j++)
{
if(!isset($row[$j])) {
$schema_insert .= "NULL".$sep;
}
elseif ($row[$j] != "") {
$schema_insert .= $sep."$row[$j]"."\r\n";
}
else {
$schema_insert .= "".$sep;
}
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}

?>


Pouvez vous m'aider
A voir également:

1 réponse

zerdg Messages postés 86 Date d'inscription jeudi 21 mai 2015 Statut Membre Dernière intervention 30 novembre 2017 2
28 mai 2015 à 12:48
j'y suis arrivé mais je n'arrive pas le faire avec des boucles :

<?php
require_once ('Classes/PHPExcel.php');
require_once ('Classes/PHPExcel/Writer/Excel2007.php');
require_once 'Classes/PHPExcel/IOFactory.php';
include ('connect.php');


$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sql = 'SELECT * FROM reponses';
$req = $bdd->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$req ->execute();

$sheet->setCellValueByColumnAndRow(0,1,'identifiant' );
$sheet->setCellValueByColumnAndRow(1,1,'code' );
$sheet->setCellValueByColumnAndRow(2,1,'sexe' );
$sheet->setCellValueByColumnAndRow(3,1,'age' );
$sheet->setCellValueByColumnAndRow(4,1,'composition' );
$sheet->setCellValueByColumnAndRow(5,1,'nombreEnfants_3ans' );
$sheet->setCellValueByColumnAndRow(6,1,'nombreEnfants_matpri' );
$sheet->setCellValueByColumnAndRow(7,1,'nombreEnfants_sec' );
$sheet->setCellValueByColumnAndRow(8,1,'nombreTotalEnfants' );
$sheet->setCellValueByColumnAndRow(9,1,'modeGardeActuel' );
$sheet->setCellValueByColumnAndRow(10,1,'depose' );
$sheet->setCellValueByColumnAndRow(11,1,'organisation' );
$sheet->setCellValueByColumnAndRow(12,1,'choixModeGarde' );
$sheet->setCellValueByColumnAndRow(13,1,'employeur' );
$sheet->setCellValueByColumnAndRow(14,1,'raison' );
$sheet->setCellValueByColumnAndRow(15,1,'maniere' );
$sheet->setCellValueByColumnAndRow(16,1,'horairesLundi' );
$sheet->setCellValueByColumnAndRow(17,1,'horairesMardi' );
$sheet->setCellValueByColumnAndRow(18,1,'horairesMercredi' );
$sheet->setCellValueByColumnAndRow(19,1,'horairesJeudi' );
$sheet->setCellValueByColumnAndRow(20,1,'horairesVendredi' );
$sheet->setCellValueByColumnAndRow(21,1,'horairesLundiP' );
$sheet->setCellValueByColumnAndRow(22,1,'horairesMardiP' );
$sheet->setCellValueByColumnAndRow(23,1,'horairesMercrediP' );
$sheet->setCellValueByColumnAndRow(24,1,'horairesJeudiP' );
$sheet->setCellValueByColumnAndRow(25,1,'horairesVendrediP' );
$sheet->setCellValueByColumnAndRow(26,1,'attentes' );
$sheet->setCellValueByColumnAndRow(27,1,'estivale' );
$sheet->setCellValueByColumnAndRow(28,1,'residence' );
$sheet->setCellValueByColumnAndRow(29,1,'remarques' );
$sheet->setCellValueByColumnAndRow(30,1,'dateHeure' );


$ligne = 2;
while($data = $req->fetch()){
$colonne=0;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['identifiant']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['code']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['sexe']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['age']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['composition']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['nombreEnfants_3ans']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['nombreEnfants_matpri']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['nombreEnfants_sec']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['nombreTotalEnfants']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['modeGardeActuel']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['depose']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['organisation']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['choixModeGarde']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['employeur']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['raison']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['maniere']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesLundi']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesMardi']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesMercredi']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesJeudi']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesVendredi']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesLundiP']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesMardiP']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesMercrediP']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesJeudiP']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['horairesVendrediP']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['attentes']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['estivale']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['residence']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['remarques']);$colonne++;
$sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['dateHeure']);$colonne++;


$ligne++;//ligne suivante
}

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="tableau.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');

$objWriter = PHPExcel_IOFactory::createWriter($workbook, 'Excel5');
$objWriter->save('php://output');
exit;
0