2011-12-15 1 views
2

J'ai un fichier PHP qui a une variable nommée $data. Maintenant, je fais écho à cette variable sur la page. Cela fonctionne bien, mais je veux écrire cette variable dans un fichier doc. Comment est-ce que tu fais ça?comment générer un fichier doc en utilisant php

J'utilise la méthode suivante pour générer le document mais cela ne fonctionne pas.

$fh = fopen('viewAppraisalDetailDoc.doc',"w+"); 
fwrite($fh, $data); 
$file = 'viewAppraisalDetailDoc.doc'; 

if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/msword'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
    exit; 
} 

Répondre

0
<?php 
// starting word 
    $word = new COM("word.application") or die("Unable to instantiate Word"); 
    echo "Loaded Word, version {$word->Version}\n"; 

    //bring it to front 
     $word->Visible = 1; 

    //open an empty document 
    $word->Documents->Add(); 

     //do some weird stuff 
     $word->Selection->TypeText("This is a test..."); 
    $word->Documents[1]->SaveAs("Useless test.doc"); 

    //closing word 
     $word->Quit(); 

    //free the object 
    $word = null; 
    ?> 

Je ne suis pas sûr que cela fonctionnera ou non, comme je l'ai pas essayé,

passer par manuel php COM, http://www.php.net/manual/en/class.com.php

+2

Très mauvaise idée, car il est un extension Windows uniquement. – Maerlyn

+1

Généralement Tous les serveurs sont sous Unix ou Linux. Alors comment est-ce utile? –

Questions connexes