PHPExcel

Librairie OpenSource pour générer, en PHP, des fichiers Excel.

https://github.com/PHPOffice/PHPExcel

Un exemple d’utilisation (source https://www.developpez.net/forums/d1711806/php/langage/phpexcel-choisir-chemin-telechargement/); pas forcément le plus propre mais cela donne une idée.

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
date_default_timezone_set('Europe/London');
require_once dirname(__FILE__) . '/PHPExcel-1.8/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator('Maarten Balliauw')
    ->setLastModifiedBy('Maarten Balliauw')
    ->setTitle('PHPExcel Test Document')
    ->setSubject('PHPExcel Test Document')
    ->setDescription('Test document for PHPExcel, generated using PHP classes.')
    ->setKeywords('office PHPExcel php')
    ->setCategory('Test result file');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'nom d usage')
    ->setCellValue('B1','prenom')
    ->setCellValue('C1','Nom de famille')
    ->setCellValue('D1','date de naissance')
                              ->setCellValue('E1','categorie');

include('connection.php');

mysql_connect("localhost", "root", "");
mysql_select_db("gestion_compte");

$reponse = mysql_query("SELECT * FROM conge");
$i=2;
while($donnees = mysql_fetch_array($reponse)) { 
    $prenom =  $donnees['prenom'];
    $nom_dusagee =  $donnees['nom_dusage'];
    $nom_de_famille =  $donnees['nom_famille'];
    $date_de_naissance =  $donnees['date_naissance'];
    $categorie = $donnees['categorie'];
    $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, "$nom_dusagee")
        ->setCellValue('B' . $i, "$prenom")
        ->setCellValue('C' . $i, "$nom_de_famille")
        ->setCellValue('D' . $i, "$date_de_naissance")
    ->setCellValue('E' . $i, "$categorie");
    $i++;
}
$objPHPExcel->getActiveSheet()->getStyle('A1:E1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->setAutoFilter($objPHPExcel->getActiveSheet()->calculateWorksheetDimension());
$objPHPExcel->setActiveSheetIndex(0);
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();

header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="Tableau_excel.xlsx"');
$objWriter->save('php://output');
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;