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)];
}
}
...全文
540 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
umbrella1984 2005-07-21
  • 打赏
  • 举报
回复
在COM里做转换。。
nullfox 2005-07-21
  • 打赏
  • 举报
回复
在phpclasses上看到过一个,php4的,问题也是生成中文word就乱码。
fzjw 2005-07-11
  • 打赏
  • 举报
回复
唠叨老大,俺的开发环境是Windows Server 2003 + Office 2003

我要出去一段时间,请各位继续,等我回来继续和大家讨论
xuzuning 2005-07-11
  • 打赏
  • 举报
回复
尝试转换成以下编码
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");
fzjw 2005-07-11
  • 打赏
  • 举报
回复
用Iconv函数做编码转换后,一样存在这样的问题。
fzjw 2005-07-11
  • 打赏
  • 举报
回复
标题
内容:
潬瑡2005-07-11
表格

___________________________________________

上面是执行结果,生成的Word文档就是那样的,郁闷 :(

<?php

require_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");

?>
wen8u8 2005-07-11
  • 打赏
  • 举报
回复
藏起来。看看。
mikespook 2005-07-11
  • 打赏
  • 举报
回复
中文乱码的话可能是编码问题引起的~~
在writeText方法里对$content的编码转换为utf-8
keaizhong 2005-07-11
  • 打赏
  • 举报
回复
没有怎么用过COM,它本身是个类,再做一个类来控制它,是不是真的有意义?
关注一下。。
  • 打赏
  • 举报
回复
测试出现一下错误:

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

indexroot 2005-07-10
  • 打赏
  • 举报
回复
UP
谢谢
fzjw (冰冰)
jxyuhua 2005-07-10
  • 打赏
  • 举报
回复
fzjw 2005-07-09
  • 打赏
  • 举报
回复
目前存在最大的问题是中文部分乱码
找到的相关资料比较少,功能还比较单一,请大家多提意见 :)
fzjw 2005-07-09
  • 打赏
  • 举报
回复

/**
* 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");
?>

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧