PHP5调用COM生成Word文档的类,请大家帮完善一下 :-)

word.inc

word = new COM("word.application"); $this->word->WindowState = 0; // 0: default; 1: maximize; 2: minimize; $this->word->Visible = true; // true: display the MSWord window // false: hide the MSWord window if ("" == $this->savePath) { if (!is_dir($path)) { $this->error(" ``` ".$path." ``` is not a directory."); } $this->savePath = $path; } } /** * Get the MSWord version and language code. * * @access public * @return void */ public function getInfo() { printf (" ```

MSWord information

``` "); printf (" ```

MSWord version: %s

``` \n",$this->word->Version()); printf (" ```

MSWord language code: %s

``` \n",$this->word->System->Country()); } /** * Create a new document. * * @access public * @return boolean */ public function newDoc() { $this->doc = $this->word->Documents->Add(); return true; } /** * document page setup * * @param float $topMargin in points * @param float $rightMargin in points * @param float $bottomMargin in points * @param float $leftMargin in points * @access public * @return boolean */ public function pageSetup($topMargin,$rightMargin,$bottomMargin,$leftMargin) { $this->doc->PageSetup->TopMargin = $topMargin; $this->doc->PageSetup->RightMargin = $rightMargin; $this->doc->PageSetup->BottomMargin = $bottomMargin; $this->doc->PageSetup->LeftMargin = $leftMargin; return true; } /** * Open a document * * @param string $fileName document name * $fileName="C:\worddocuments\test.doc" * @access public * @return boolean */ public function openDoc($fileName) { try { $this->word->Documents->Open($fileName); } catch (Exception $e) { echo " ```
".$e->getMessage()."
``` "; } return true; } /** * To Write a new MSWord document and to change the font * * @param string $fontName * @param int $fontSize * @param boolean $fontBold * @param boolean $fontItalic * @param boolean $fontUnderline * @param int $alignment * @param string $content * @access public * @return void */ public function writeText($fontName,$fontSize,$fontBold,$fontItalic, $fontUnderline,$alignment,$content) { $this->word->Selection->Font->Name = $fontName; $this->word->Selection->Font->Size = $fontSize; $this->word->Selection->Font->Bold = $fontBold; $this->word->Selection->Font->Italic = $fontItalic; $this->word->Selection->Font->Underline = $fontUnderline; $this->word->Selection->ParagraphFormat->Alignment = $alignment; $this->word->Selection->TypeText($content."\n"); } /** * Replace words in a MSWrod document * * @param string $str * @param string $replace_str * @access public * @return void */ public function replaceText($str,$replace_str) { $this->word->Selection->Find->Execute($str,false,true,false,false,false, true,1,false,$replace_str,2); } /** * Create a table in MSWord * * @param int $cols \--------------------------------------------------------------- UP 谢谢 fzjw (冰冰) \--------------------------------------------------------------- 收 \--------------------------------------------------------------- 没有怎么用过COM,它本身是个类,再做一个类来控制它,是不是真的有意义? 关注一下。。 \--------------------------------------------------------------- 中文乱码的话可能是编码问题引起的~~ 在writeText方法里对$content的编码转换为utf-8 \--------------------------------------------------------------- 藏起来。看看。 \--------------------------------------------------------------- 尝试转换成以下编码 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 110 110行:$this->word->Selection->TypeText($content."\n"); \--------------------------------------------------------------- 测试出现一下错误: 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 \--------------------------------------------------------------- 在phpclasses上看到过一个,php4的,问题也是生成中文word就乱码。 \--------------------------------------------------------------- 在COM里做转换。。
Published At
Categories with Web编程
comments powered by Disqus