Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Actuellement, je travaille sur un projet d'étude, réalisé en Php-Zend Framework. Je dois cependant générer des fichiers Excel avec des données de ma base de donnée MySql.
Pour cela, j'ai essayé le tuto http://g-ernaelsten.developpez.com/tutoriels/excelphp/
j'essai de générer un tableur Excel; au moyen de l'action d'un controlleur pour pouvoir ecrire "toto" dans la case C2 de mon fichier, comme suggéré dans l'article:
function excelAction() { #faire différents traitements, puis appeler la méthode pour le fichier Excel $this->Excel(); } private function Excel() { /** * On génère le fichier de base */ $workbook = new Spreadsheet_Excel_Writer(); $workbook->send('base.xls'); $worksheet = $workbook->addWorksheet(); $worksheet->write(1,2,'toto'); $workbook->close(); $workbook->sendFile(); }
mais dans le fichier genere j'ai des erreurs que je ne sais comment les corriges :
<b>Deprecated</b>: Assigning the return value of new by reference is deprecated in <b>C:\wamp\www\SamaxSoft\library\PEAR.php</b> on line <b>569</b><br /> <br /> <b>Deprecated</b>: Assigning the return value of new by reference is deprecated in <b>C:\wamp\www\SamaxSoft\library\PEAR.php</b> on line <b>572</b><br /> <br /> <b>Deprecated</b>: Assigning the return value of new by reference is deprecated in <b>C:\wamp\www\SamaxSoft\library\Spreadsheet\Excel\Writer\Workbook.php</b> on line <b>186</b><br /> <br /> <b>Deprecated</b>: Assigning the return value of new by reference is deprecated in <b>C:\wamp\www\SamaxSoft\library\Spreadsheet\Excel\Writer\Workbook.php</b> on line <b>195</b><br /> <br /> <b>Strict Standards</b>: Non-static method OLE::Asc2Ucs() should not be called statically, assuming $this from incompatible context in <b>C:\wamp\www\SamaxSoft\library\Spreadsheet\Excel\Writer\Workbook.php</b> on line <b>586</b><br /> <br /> <b>Strict Standards</b>: Non-static method OLE::Asc2Ucs() should not be called statically, assuming $this from incompatible context in <b>C:\wamp\www\SamaxSoft\library\OLE\PPS\Root.php</b> on line <b>52</b><br /> <br /> <b>Strict Standards</b>: Non-static method OLE::LocalDate2OLE() should not be called statically, assuming $this from incompatible context in <b>C:\wamp\www\SamaxSoft\library\OLE\PPS.php</b> on line <b>186</b><br /> <br /> <b>Strict Standards</b>: Non-static method OLE::LocalDate2OLE() should not be called statically, assuming $this from incompatible context in <b>C:\wamp\www\SamaxSoft\library\OLE\PPS.php</b> on line <b>187</b><br /> <br /> <b>Strict Standards</b>: Non-static method OLE::LocalDate2OLE() should not be called statically, assuming $this from incompatible context in <b>C:\wamp\www\SamaxSoft\library\OLE\PPS.php</b> on line <b>186</b><br /> <br /> <b>Strict Standards</b>: Non-static method OLE::LocalDate2OLE() should not be called statically, assuming $this from incompatible context in <b>C:\wamp\www\SamaxSoft\library\OLE\PPS.php</b> on line <b>187</b><br />
plus des charabia :
'Sheet1 l toto> ¶
j'ai besoin d'aide Urg,
Merci d'avance.
Hors ligne
il y a beaucoup plus simple
tu fais un contrôleur exactement comme pour un page
tu place tes données dans la vue comme pour une page comme si c'était une table html
tu ajoute un header mime type applicaion/excel
tu ajout un head pour dire que tu attache un fichier et tu donne un nom à ton fichier
reste à faire la vue
tu ouvre excel tu crée un tableau comme celui que tu veux obtenir
pense à ne mettre que les onglets nécessaires.
tu enregistre ton classeur excel en xml avec enregistrer sous
tu place ton fichier dans ton dossier de vue et tu le nomme comme pour une page
monAction.phtml
tu l'ouvre avec ton éditeur de code
dans l'entête tu place l'entête xml <?....?> dans un echo
<?php echo('<?........?>'); ?>
sinon php va essayer d'exécuter l'entête
tu descend dans le xml et tu vas trouver un truc qui ressemble à une table html
sauf qu'à la place de tr et td c'est row et cell
il te reste comme pour une table HTML à remplir la table avec les données de la vue.
Attention dans le tag TABLE il y un attribut contenant le nombre total de ligne
A+JYT
Hors ligne
je pense pas avoir tous compris, mais bon je sais que je suis pas loin, voila ce que j'ai fait.
cote controlleur :
function excelAction() { $personne = new Personne(); $db = Zend_Registry::get('dbAdapter'); $select = $db->select(); $select->from(array('p' => 'personne'), '*'); $select->join(array('c' => 'cohtion'), 'p.NUMPERSONNE = c.NUMPERSONNE', '*'); $select->join(array('pa' => 'paiement'), 'c.NUMPAIEMENT = pa.NUMPAIEMENT', '*'); $sql = $select->__toString(); $this->view->personne = $db->fetchAll($sql); }
cote vue :
<html> <head> <title><?php echo $this->escape($this->title); ?></title> </head> <body> <?php echo('<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?>'); ?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Author>PC_Home</Author> <LastAuthor>PC_Home</LastAuthor> <Created>2009-11-04T10:31:08Z</Created> <LastSaved>2009-11-04T10:37:46Z</LastSaved> <Version>12.00</Version> </DocumentProperties> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>11715</WindowHeight> <WindowWidth>18615</WindowWidth> <WindowTopX>240</WindowTopX> <WindowTopY>315</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID="s63"> <Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/> </Style> </Styles> <Worksheet ss:Name="Feuil1"> <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="60" ss:DefaultRowHeight="15"> <Column ss:AutoFitWidth="0" ss:Width="84" ss:Span="1"/> <Column ss:Index="3" ss:AutoFitWidth="0" ss:Width="76.5"/> <Column ss:AutoFitWidth="0" ss:Width="101.25"/> <Column ss:AutoFitWidth="0" ss:Width="135.75"/> <?php foreach($this->personne as $personne) : ?> <Row> <Cell ss:StyleID="s63"><Data ss:Type="String"><?php echo $this->escape($personne['NOM']);?></Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['PRENOM']);?></Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['MONTANT']);?></Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['TYPE']);?> </Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['NBRPAIEMENT']);?></Data></Cell> </Row> <?php endforeach; ?> </Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <PageSetup> <Header x:Margin="0.3"/> <Footer x:Margin="0.3"/> <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> </PageSetup> <Selected/> <Panes> <Pane> <Number>3</Number> <ActiveRow>1</ActiveRow> <ActiveCol>3</ActiveCol> </Pane> </Panes> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> </Workbook> </body> </html>
a l'affichage de la vue ca donne ceci :
PC_Home PC_Home 2009-11-04T10:31:08Z 2009-11-04T10:37:46Z 12.00 11715 18615 240 315 False False elak chaf 45.00 Liquide 3 jacquet thoor 20.00 Liquide 5 elak sami 40.00 Virment 4 e e 20.00 Liquide 7 3 1 3 False False
Dernière modification par sm (04-11-2009 12:09:40)
Hors ligne
c'est bien ça il te manque juste à mettre des header HTTP dans ton action pour non pas ouvrir le xml dans le navigateur mais l'envoyer à télécharger
Hors ligne
j'ai une erreur de get() a cette ligne "$reponse = $http->get();"
Fatal error: Call to undefined method Zend_Http_Client::get() in C:\wamp\www\SamaxSoft\application\controllers\ExporterController.php on line 16
voila comment je gère mon action
function excelAction() { $http = new Zend_Http_Client(); $http->setUri('http://localhost/SamaxSoft/exporter/excel'); $reponse = $http->get(); $personne = new Personne(); $db = Zend_Registry::get('dbAdapter'); $select = $db->select(); $select->from(array('p' => 'personne'), '*'); $select->join(array('c' => 'cohtion'), 'p.NUMPERSONNE = c.NUMPERSONNE', '*'); $select->join(array('pa' => 'paiement'), 'c.NUMPAIEMENT = pa.NUMPAIEMENT', '*'); $select->where('p.NUMROLE IN (?)', array(2, 3)); $sql = $select->__toString(); $this->view->personne = $db->fetchAll($sql); $enTetesDeReponse = $reponse->getHeaders(); }
mais je vois pas comment corrigé l'erreur.
Dernière modification par sm (06-11-2009 15:43:10)
Hors ligne
beh, c'est simple, la fonction get n'exsite pas
essaie plus request
Hors ligne
je dois cree ma propre methode get() alors
Hors ligne
Comme ça tu récupères ce que tu désires :
$http = new Zend_Http_Client(); $http->setUri('http://localhost/SamaxSoft/exporter/excel'); $reponse = $http->request();
Hors ligne
mais ca genere pas le fichier excel, ca fais que afficher le resultat de la requette
PC_Home PC_Home 2009-11-04T10:31:08Z 2009-11-04T10:37:46Z 12.00 11715 18615 240 315 False False elak chafik 45.00 Liquide 3 jacquet thoorens 20.00 Liquide 5 elak sami 40.00 Virment 4 3 1 3 False False
Hors ligne
c'est bien ça il te manque juste à mettre des header HTTP dans ton action pour non pas ouvrir le xml dans le navigateur mais l'envoyer à télécharger
comment l'envoyer à télécharger ?
j'ai parcouru la doc Zend_HTTP et j'avoue que les exemples ne sont pas claire pour moi. deja le mot http ca me rend mal a l'aise
Hors ligne
enfin ca marche, juste un petit truc a l'ouverture du fichier j'ai un message (le format de fichier que vous tentez d'ouvrir monfichier.xls, est différent de celui spécifie par l'extension de fichier) et dedans les resultats ne sont afficher que dans une case, est ce que ca veut dire que je dois ajouter une boucle for dans le code xml ?
Dernière modification par sm (06-11-2009 23:27:37)
Hors ligne
fait une recherche sur le net sur les entête http
il te faut envoyer l'entête d'attachement pour dire au navigateur que c'est un fichier à télécharger
et un entête mime pour donner le type du fichier
A+JYT
Hors ligne
je donne bien le type de fichier "application/vnd.ms-excel" dans mes headers et le telechargement se fais sans probleme
$date = new Zend_Date(); $nom = $date->get(Zend_Date::W3C); header("Pragma: public"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: pre-check=0, post-check=0, max-age=0"); header("Pragma: no-cache"); header("Expires: 0"); header("Content-Transfer-Encoding: none"); header("Content-type: application/vnd.ms-excel"); header("Content-type: application/x-msexcel"); header("Content-Disposition: attachment; filename=\"$nom\"");
le probleme est dans le fichier telecharger, tous les donnees sont écrites dans une case alors que j'aimerais que ca soit dans des cases separé
Dernière modification par sm (08-11-2009 00:10:56)
Hors ligne
normal tu as mis du html dans ton fichier c'est du XML que tu produit il faut ABSOLUMENT qu'il soit conforme à ce qu'attends Excel
<html> <head> <title><?php echo $this->escape($this->title); ?></title> </head> <body> ....
ton phtml ne doit contenir que le XML à produire
A+JYT
Hors ligne
tu ouvre excel tu crée un tableau comme celui que tu veux obtenir pense à ne mettre que les onglets nécessaires. tu enregistre ton classeur excel en xml avec enregistrer sous
enregistrer sous quelle format "donnees xml" ou "feuille de calcul xml 2003" ?
la j'utilise le format feuille de calcul xml 2003.
Dernière modification par sm (08-11-2009 20:48:29)
Hors ligne
je crois avoir un probleme sur cette ligne
ss:ExpandedRowCount="1"
il faut qu'il soit 'incrémenté, mais je ne cois pas comment dans mon cas.
Hors ligne
c'est bien feuille de calcul
mais tu ne le mets pas dans un body html tu utilise le xml comme modèle
ss:ExpandedRowCount="1"
pour ça il de suffit de faire un array_count sur $this->personne
attention de compter aussi la ligne d'en-tête si tu en as une
A+JYT
Hors ligne
maintenant j'ai une erreur.
<?php echo('<?xml version="1.0" encoding="ISO-8859-1"?> <?mso-application progid="Excel.Sheet"?>'); ?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID="s63"> <Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/> </Style> </Styles> <Worksheet ss:Name="Feuil1"> <?php foreach($this->personne as $personne) : ?> <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount=<?php (int)array_count($personne); ?> x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="60" ss:DefaultRowHeight="15"> <Column ss:AutoFitWidth="0" ss:Width="84" ss:Span="1"/> <Column ss:Index="3" ss:AutoFitWidth="0" ss:Width="76.5"/> <Column ss:AutoFitWidth="0" ss:Width="101.25"/> <Column ss:AutoFitWidth="0" ss:Width="135.75"/> <Row> <Cell ss:StyleID="s63"><Data ss:Type="String"><?php echo $this->escape($personne['NOM']);?></Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['PRENOM']);?></Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['MONTANT']);?></Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['TYPE']);?> </Data></Cell> <Cell><Data ss:Type="String"><?php echo $this->escape($personne['NBRPAIEMENT']);?></Data></Cell> </Row> </Table> <?php endforeach; ?> </Worksheet> </Workbook>
si vous pouvez m'aidez avant mardi ca serais super, Merci d'avance.
Hors ligne
le foreach est sur les row le worksheet ne contient qu'une table
par contre si tu as besoin tu peut mettre plusieur worksheet dans ton workbook
Hors ligne
voila le probleme que je n'arrive pas a resoudre, aucune idée pour supprimer cet entête lors du telechargement du fichier
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title></title> <link rel="stylesheet" type="text/css" media="screen" href="/SamaxSoft/public/css/site.css" /> </head> <body> <div id="content"> <h1></h1> <?xml version="1.0"?> ...
Dernière modification par sm (29-11-2009 15:59:16)
Hors ligne