编程语言
首页 > 编程语言> > 如何使用php生成doc文件

如何使用php生成doc文件

作者:互联网

我有一个PHP文件,其中包含一个名为$data的变量.现在,我在页面上回显此变量.它工作正常,但我想在doc文件中写入此变量.你怎么做到这一点?

我正在使用以下方法生成文档,但无法正常工作.

$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;
}

解决方法:

 <?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;
  ?>

我不确定是否会奏效,因为我还没有尝试过,

通过php COM手册,
http://www.php.net/manual/en/class.com.php

标签:php,doc-2
来源: https://codeday.me/bug/20191011/1896313.html