Il y a beaucoup de possibilités de sortir des documents Excel. Mais la meilleure que j'ai trouvé, c'est ... le HTML
En effet, Excel (et même Word) sont capables de générer des fichiers HTML et donc de les lire. Pour voir comment ça marche, il faut prendre un fichier Excel, l'enregistrer au format HTML, puis étudier le fichier généré. On peut ainsi générer des couleurs, des polices de caractères, ...
Donc, c'est facile à tester : tu fais du HTML et ensuite tu colles un entête disant que c'est du Excel. Et c'est encore plus spectaculaire avec Word : tu génères un document HTML, tu colles l'entête HTML/Word et tu as un document Word.
Voici un exemple d'un code qui sort un tableau à partir d'une requête dans une base de données (rassurez vous, j'ai juste fait un copier coller du code HTML à partir d'un fichier enregistré au format HTML par Excel)
<?php
$name = "fichier-".date("ymd-Hi");
header( 'Pragma: public' );
header( 'Content-Type: application/excel' );
header( 'Content-Disposition: attachment; filename="'.$name.'.xls"' );
?>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
<link rel=File-List href="Classeur2_fichiers/filelist.xml">
<link rel=Edit-Time-Data href="Classeur2_fichiers/editdata.mso">
<link rel=OLE-Object-Data href="Classeur2_fichiers/oledata.mso">
<style>
<!--
table {mso-displayed-decimal-separator:"\,"; mso-displayed-thousand-separator:" ";}
@page {margin:.98in .79in .98in .79in; mso-header-margin:.49in; mso-footer-margin:.49in;}
tr {mso-height-source:auto;}
col {mso-width-source:auto;}
br {mso-data-placement:same-cell;}
.style0 {mso-number-format:General; text-align:general; vertical-align:bottom; white-space:nowrap;
mso-rotate:0; mso-background-source:auto; mso-pattern:auto; color:windowtext;
font-size:8.0pt; font-weight:400; font-style:normal; text-decoration:none;
font-family:Arial; mso-generic-font-family:auto; mso-font-charset:0;
border:none; mso-protection:locked visible; mso-style-name:Normal;
mso-style-id:0;}
td {mso-style-parent:style0; padding-top:1px; padding-right:1px; padding-left:1px;
mso-ignore:padding; color:windowtext; font-size:8.0pt; font-weight:400;
font-style:normal; text-decoration:none; font-family:Arial; mso-generic-font-family:auto;
mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom;
border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible;
white-space:nowrap; mso-rotate:0;}
.xl24 {mso-style-parent:style0; mso-number-format:"\@";}
.xl25 {mso-style-parent:style0; font-weight:700; mso-number-format:"\@";}
-->
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Feuil1</x:Name>
<x:WorksheetOptions>
<x:DefaultColWidth>10</x:DefaultColWidth>
<x:Selected/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
<x:RangeSelection>$A$1:$A$1</x:RangeSelection>
</x:Pane>
</x:Panes>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>8850</x:WindowHeight>
<x:WindowWidth>18795</x:WindowWidth>
<x:WindowTopX>120</x:WindowTopX>
<x:WindowTopY>135</x:WindowTopY>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><![endif]-->
</head>
<table x:str border=0 cellpadding=0 cellspacing=0 width=160 style='border-collapse:collapse;table-layout:fixed;width:120pt'>
<tr height=17 style='height:12.75pt'>
<?php
//Affichage des entêtes de colonne
while (list($key, $val) = each($cols)) {
echo "<td height=17 class=xl25 width=80 style='height:12.75pt;width:60pt'>$val</td>";
}
?>
</tr>
<?
//Affichage des valeurs dans les cellules
while (list($key, $ligne) = each($rs)) {
set_time_limit(30);
echo " <tr height=17 style='height:12.75pt'>";
while (list($key, $val) = each($ligne)) {
echo "<td height=17 class=xl24 width=80 style='height:12.75pt;width:60pt'>$val</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>
Seul bémol. Malgré son extension xls, ce n'est pas un vrai fichier Excel. Une fois que le fichier est ouvert dans Excel, il faut dire à l'utilisateur de faire "enregistrer sous ..." format xls si par exemple il veut faire un lien odbc.