/** 
    * Save the document 
    * 
    * @param $fileName 
    * @access public 
    * @return void 
    */ 
   public function saveAs($fileName) { 
       if ("" == $fileName) { 
           $fileName = date("YmdHis"); 
       } 
       printf ("<pre>File save as directory: %s</pre>",$this->savePath); 
       $this->word->Documents[1]->SaveAs($this->savePath.$fileName); 
   }    /** 
    * @param $msg              error message 
    * @access private 
    * @return void 
    */ 
   private function error($msg) { 
       printf("<h1>Error:</h1>\n<hr size=\"1\" color=\"#CCCCCC\" />\n<pre>%s</pre>",$msg); 
       exit(); 
   }    /** 
    * Class destructor. 
    * 
    * @access public 
    */ 
   public function __destruct() { 
       //Close Word 
       $this->word->Quit(); 
   } 

?>test.php
<?php 
require_once "word.inc"; $path = "C:/worddocuments/"; $word = new Word($path); // Create new document 
$word->newDoc(); //setup page margin 
$word->pageSetup(90,60,80,80); $title = "Title"; 
$content = "This is a test. 
......"; //write content. 
$word->writeText("Arial",20,true,false,false,1,$title); 
$word->writeText("Times New Roman",10,false,false,false,0,$content); 
$word->writeText("Times New Roman",10,false,false,false,2,date("Y-m-d")); 
$word->writeText("Times New Roman",10,false,false,false,0,""); $word->writeText("Times New Roman",10,true,false,false,1,"Table Title"); 
//Create tables 
$word->createTable(4,5); 
//write in cells 
$word->writeCell(1,1,"Times New Roman",10,true,false,false,1,"ID."); 
$word->writeCell(1,2,"Times New Roman",10,true,false,false,1,"NAME"); 
$word->writeCell(1,3,"Times New Roman",10,true,false,false,1,"UNIT"); 
for ($i=2; $i<=4; $i++) { 
   $word->writeCell($i,1,"Times New Roman",10,false,false,false,1,($i-1)); 
} $properties = array ( 
   0  => "Title", 
   1  => "Subject", 
   2  => "fengzhiju", 
   3  => "test", 
   4  => "Comments", 
   5  => "Template", 
   6  => "LastAuthor", 
   7  => "Revision", 
   8  => "AppName", 
   9  => "TimeLastPrinted", 
   10 => "TimeCreated", 
   11 => "TimeLastSaved", 
   12 => "VBATotalEdit", 
   13 => "Pages", 
   14 => "Words", 
   15 => "Characters", 
   16 => "Security", 
   17 => "Category", 
   18 => "Format", 
   19 => "Manager", 
   20 => "Company", 
   21 => "Bytes", 
   22 => "Lines", 
   23 => "Paras", 
   24 => "Slides", 
   25 => "Notes", 
   26 => "HiddenSlides", 
   27 => "MMClips", 
   28 => "HyperlinkBase", 
   29 => "CharsWSpaces" 
); $word->properties($properties); //save document. 
$word->saveAs("test"); ?>replace_test.php
<?php 
require_once "word.inc"; $path = "C:/worddocuments/"; $word = new Word($path); $word->openDoc("C:/worddocuments/test.doc"); $str = "Title"; 
$replace_str = "New Title"; $word->replaceText($str,$replace_str); 
$word->saveAs("test"); 
?>

解决方案 »

  1.   

    UP
    谢谢
    fzjw (冰冰)
      

  2.   

    没有怎么用过COM,它本身是个类,再做一个类来控制它,是不是真的有意义?
    关注一下。。
      

  3.   

    中文乱码的话可能是编码问题引起的~~
    在writeText方法里对$content的编码转换为utf-8
      

  4.   

    标题
      内容:
     潬瑡2005-07-11
    表格
     崸
    ___________________________________________上面是执行结果,生成的Word文档就是那样的,郁闷 :(<?phprequire_once "word.inc";$path = "E:/wordclass/";
    $word = &new Word($path);// Create new document
    $word->newDoc();//setup page margin
    $word->pageSetup(90,60,80,80);$title = "标题\n";
    $content = "内容:\n";//write content.
    $word->writeText("Times New Roman",20,true,false,false,1,$title);
    $word->writeText("Times New Roman",12,false,false,false,0,$content);
    $word->writeText("Times New Roman",12,false,false,false,2,date("Y-m-d")."\n");
    $word->writeText("Times New Roman",12,false,false,false,0,"");
    $word->writeText("Times New Roman",12,true,false,false,1,"表格\n");$word->saveAs("test.doc");?>
      

  5.   

    用Iconv函数做编码转换后,一样存在这样的问题。
      

  6.   

    尝试转换成以下编码
    UCS-2
    UCS-2BE
    UCS-2LE好象应该是UCS-2LE,但你的程序有问题。在word2003环境下报错,无法测试!
    Fatal error: Call to a member function TypeText() on a non-object in ... word.inc on line 110110行:$this->word->Selection->TypeText($content."\n"); 
      

  7.   

    唠叨老大,俺的开发环境是Windows Server 2003 + Office 2003我要出去一段时间,请各位继续,等我回来继续和大家讨论
      

  8.   

    测试出现一下错误:Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft Word
    Description: 无法打开宏储存。' in E:\Web\www\Temp\word\word.inc.php:50 Stack trace: #0 E:\Web\www\Temp\word\word.inc.php(50): variant->Add() #1 E:\Web\www\Temp\word\test.php(9): Word->newDoc() #2 {main} thrown in E:\Web\www\Temp\word\word.inc.php on line 50
      

  9.   

    在phpclasses上看到过一个,php4的,问题也是生成中文word就乱码。