问题
I have a PHP file that has a variable named $data. Now I echo this variable on the page. It works fine but I want to write this variable in a doc file. How do you do this?
I am using the following method to generate the doc but it's not working.
$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;
}
回答1:
<?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;
?>
I am not sure whether it will work or not, as I have not tried it,
go through php COM manual, http://www.php.net/manual/en/class.com.php
来源:https://stackoverflow.com/questions/8516222/how-to-generate-doc-file-using-php