how to generate doc file using php

雨燕双飞 提交于 2019-12-20 03:04:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!