Comment formater le file Excel avec des styles, des fonts, des colors, des arrays, etc. avec PHP pur?

J'ai besoin d'un moyen d'écrire un file Excel avec un format via Pure PHP. AVERTISSEMENT: S'il vous plaît, ce Q n'est pas sur les bibliothèques, les plugins, etc. – ne pas déranger. J'ai beaucoup googlé et je n'ai pas trouvé la réponse, mais dans un article sur StackOverflow, un gars a mentionné les solutions de format HTML / CSS format format HTML qui ne fonctionnaient pas et j'ai essayé de le faire, mais dans le file qui a été enregistré des colonnes où rempli de balises et rien ne se produit. Peut-être y at-il des professionnels qui peuvent m'aider avec un code pur, écrire un Excel via PHP, HTML, CSS ou quelque chose.

Et à nouveau avec l'exemple, j'ai déjà utilisé un concept comme celui-ci qui n'a pas aidé:

// headers for excel $headers .= "<div style='font:bold 14px Times;'>Журнал остатков и инкассаций</div>\n"; $headers .= "Начало периода\t ".date('dmY')."0:00 \n"; $headers .= "Начало периода\t ".date('dmY')."23:59 \n\n"; $headers .= "Терминал\t"; $headers .= "Место расположения\t"; $headers .= "Дата\t"; $headers .= "Время\t"; $headers .= "Сумма инкассации\t"; $headers .= "Банкноты\t"; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data .= $rowCollection->Name."\t".$rowCollection->Address."\t" .$rowCollection->TerminalNotes."\t".$rowCollection->TerminalAmount ."\t".$rowCollection->DayAmountAll."\t" .$rowCollection->LastPaymentTime."\n"; } } $fileName = 'collection'.date('d_m_Y_H_i_s').'.xls'; header("Content-type: application/vnd.ms-excel");// application/excel had also been used header("Content-Disposition: attachment; filename=$fileName"); echo iconv('utf-8', 'cp1251', "$headers\n$data"); 

J'ai utilisé non seulement des divs et des tables aussi – il n'a pas réussi.

    L'exemple suivant vous aiderait probablement:

      1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> 1 <? Php  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> En-tête 2 ('Type de contenu: application / excel');  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> En-tête 3 ('Contenu-Disposition: pièce jointe; filename = "test.xls"');  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> 4?>  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> 6 <table>  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> 7 <tr> <th> Colonne 1 </ th> <th> Colonne 2 </ th> </ tr>  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> 8 <TR> <td style = "font-size: 200%"> Réponse 1 </ td> <td style = "color: # f00"> Réponse 2 </ td> </ tr>  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> 9 <tr> <td colspan = "2" style = "font-weight: bold"> Répondez 3 avec 2 colonnes </ td> </ t r>  1 <?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?> 5 6 <table> 7 <tr><th>Column 1</th><th>Column 2</th></tr> 8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2< /td></tr> 9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t r> 10 </table> 

    Enregistrez ceci en tant que file .php sur votre server.

    Ce qu'il fait, c'est: append des en-têtes pour forcer le browser à livrer un file Excel. Et puis returnner une table à l'intérieur de celle-ci. Mon Excel le sélectionne parfaitement.

    Remarque: l'exemple HTML provient de la réponse que vous avez déjà trouvée. Je viens d'append les en-têtes au début.

    Remarque: Vous venez de modifier votre question et a ajouté un exemple. Votre exemple N'utilise PAS html! Vous livrez un file délimité par TAB où chaque ligne se termine par une nouvelle ligne.

    La bonne façon de le faire est de générer vraiment du HTML, c'est-à-dire d'utiliser une <table> au début, d'utiliser <tr> et </tr> pour chaque ligne, et de mettre les champs dans les balises <td> , puis de finir avec la fermeture </table> , donc essentiellement:

     $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; $ data = '<table>'; $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; // Excel contenu avec des terminaux surchargés $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; si (mssql_num_rows ($ resCollection)> 0) { $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; while ($ rowCollection = mssql_fetch_object ($ resCollection)) { $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; $ data. = '<tr>'; $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; $ data. = '<td>'. $ rowCollection-> Nom. "</ td> <td>". $ rowCollection-> Adresse. "</ td> <td>" $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; . $ rowCollection-> TerminalNotes. "</ td> <td>". $ rowCollection-> TerminalAmount $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; . </ td> <td> ". $ rowCollection-> DayAmountAll." </ td> <td> " $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; . $ rowCollection-> LastPaymentTime. "</ td>"; $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; $ data. = '</ tr>'; $data='<table>'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)>0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='<tr>'; $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>" .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount ."</td><td>".$rowCollection->DayAmountAll."</td><td>" .$rowCollection->LastPaymentTime."</td>"; $data.='</tr>'; } } $data.='</table>'; 

    Ensuite, modifiez votre dernière ligne vers:

     echo iconv('utf-8', 'cp1251', "$data"); 

    Si cela fonctionne, je vous suggère de modifier la sortie pour avoir les <th> tags autour de vos en-têtes de table, au lieu de la div dans votre exemple.