开发环境:
Windows Server 2003 Enterprise Edition,Microsoft Office 2003,PHP 5.0.4
word.inc
- <?php
- # +-----------------------------------------------------------------------------------------+
- # | PHP Version 5 |
- # +-----------------------------------------------------------------------------------------+
- # | Copyright (c) 1997-2005 The PHP Group |
- # +-----------------------------------------------------------------------------------------+
- # | This source file is subject to version 2.02 of the PHP license, |
- # | that is bundled with this package in the file LICENSE, and is |
- # | available at through the world-wide-web at |
- # | http://www.php.net/license/2_02.txt. |
- # | If you did not receive a copy of the PHP license and are unable to |
- # | obtain it through the world-wide-web, please send a note to |
- # | license@php.net so we can mail you a copy immediately. |
- # +-----------------------------------------------------------------------------------------+
- # | Authors: bingbing <feng0629@gmail.com> |
- # $Id: word.inc,v 0.1.0 dev 2005-7-8 23:10:19 Exp $
- #
- 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)];
- }
- }
- /**
- * 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
- // Create new document
- $word->newDoc();
- //setup page margin
- //setup page margin
- $word->pageSetup(90,60,80,80);
- $title = "Title";
- $content = "This is a test.
- ......";
- //write content.
- //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.
- //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");
?>
由于M$的东东大多不开放,无法获取其他资料,目前只能完成这些。等找到新的资料,我会继续加入新的方法