PHP5调用COM生成Word文档的类,请大家帮完善一下 :-)
fzjw 2005-07-09 08:39:14 word.inc
<?php
class Word {
/**
* document save path
* $savePath = "C:/worddocuments/";
* @param string $savePath
* @access public
*/
public $savePath = "";
/**
* Class constructor.
*
* @param string $path
* @access public
*/
public function __construct($path = "") {
$this->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("<b>".$path."</b> is not a directory.");
}
$this->savePath = $path;
}
}
/**
* Get the MSWord version and language code.
*
* @access public
* @return void
*/
public function getInfo() {
printf ("<h1>MSWord information</h1>");
printf ("<p>MSWord version: %s </p>\n",$this->word->Version());
printf ("<p>MSWord language code: %s </p>\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 "<pre>".$e->getMessage()."</pre>";
}
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
* @param int $rows
* @access public
* @return void
*/
public function createTable($cols,$rows) {
$this->table = $this->doc->Tables->Add($this->word->Selection->Range,$cols,$rows);
}
/**
* write in cells
*
* @param int $col
* @param int $row
* @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 writeCell($col,$row,$fontName,$fontSize,$fontBold,
$fontItalic,$fontUnderline,$alignment,$content) {
$this->table->Cell($col,$row)->Range->Font->Name = $fontName;
$this->table->Cell($col,$row)->Range->Font->Size = $fontSize;
$this->table->Cell($col,$row)->Range->Bold = $fontBold;
$this->table->Cell($col,$row)->Range->Font->Italic = $fontItalic;
$this->word->Selection->Font->Underline = $fontUnderline;
$this->table->Cell($col,$row)->Range->ParagraphFormat->Alignment = $alignment;
$this->table->Cell($col,$row)->Range->Text = $content;
}
/**
* Change MSWord document properties
*
* @param array $properties[]
* [0] word_Title: 0x00000001
* [1] word_Subject: 0x00000002
* [2] word_Author: 0x00000003
* [3] word_Keywords: 0x00000004
* [4] word_Comments: 0x00000005
* [5] word_Template: 0x00000006
* [6] word_LastAuthor: 0x00000007
* [7] word_Revision: 0x00000008
* [8] word_AppName: 0x00000009
* [9] word_TimeLastPrinted: 0x0000000A
* [10] word_TimeCreated: 0x0000000B
* [11] word_TimeLastSaved: 0x0000000C
* [12] word_VBATotalEdit: 0x0000000D
* [13] word_Pages: 0x0000000E
* [14] word_Words: 0x0000000F
* [15] word_Characters: 0x00000010
* [16] word_Security: 0x00000011
* [17] word_Category: 0x00000012
* [18] word_Format: 0x00000013
* [19] word_Manager: 0x00000014
* [20] word_Company: 0x00000015
* [21] word_Bytes: 0x00000016
* [22] word_Lines: 0x00000017
* [23] word_Paras: 0x00000018
* [24] word_Slides: 0x00000019
* [25] word_Notes: 0x0000001A
* [26] word_HiddenSlides: 0x0000001B
* [27] word_MMClips: 0x0000001C
* [28] word_HyperlinkBase: 0x0000001D
* [29] word_CharsWSpaces: 0x0000001E
* @access public
* @return void
*/
public function properties($properties = array()) {
if (!is_array($properties)) {
$this->error("\$properties is not a array.");
}
for ($i=1; $i<31; $i++) {
$j = strtoupper(dechex($i));
if (strlen($j) == 1) {
$j = "0x0000000".$j;
} elseif (strlen($j) == 2) {
$j = "0x000000".$j;
}
$this->word->ActiveDocument->BuiltInDocumentProperties($j)->Value = $properties[($i-1)];
}
}