admin 管理员组

文章数量: 1184232


2024年3月8日发(作者:编程scratch网站)

如何利用PHP操作Word文档

如何利用PHP操作Word文档,比如生成、修改后保存、删除等等……

答:用COM函数操作 MS Word

#实例化一个对象

$word = new COM("ation") or die("Unable to instantiate Word");

#取得并显示版本

print "Loaded Word, version {$word->Version}
";

#另一种方法去取得版本

$testversion = com_get($word->application,version);

print "Version using Com_get(): $testversion
";

#使其可见

$word->Visible = 1;

#创建新文件

$word->Documents->Add();

#写字符

$word->Selection->TypeText("This is ");

#保存

$word->Documents[1]->SaveAs("Useless ");

#关闭

$word->Quit();

?>

======================================================================================

PHP操作word

使用Word文档的书签(即在要替换内容的地方设置书签)来实现Word文档中的内容替换(替换为用户输入的信息)

如下为一部分操作Word的PHP代码:

/*

* 创建日期:

* 文 件 名:

* 作 者:

* 说 明:

* 版 本: $Revision$

* $Log$

*/

//实例化一个world对象

$office = new COM("ation") or die("Unable to instantiate Word");

if( ! $office )

showError(0, "Office 操作错误",true);

//调用Word显示文档

$office->Visible = 1;

$szFile = "d:/doc/";

#打开文档

$office->Documents->Open($szFile) or die("无法打开文件");

//Word中书签数量

$iBookmarks = $office->ActiveDocument->Bookmarks->Count;

//对所有书签循环替换

for( $i=1; $i<=$iBookmarks; $i++ )

{

//取书签对象

$Bookmark = $office->ActiveDocument->Bookmarks->Item($i);

$range = $Bookmark->Range;

/*

aBookmarkItem为替换书签值数组 $aBookmarkItem = array('PATENT_NAME'=>'', 'CUSTOMER_NAME'=>'',......)

数组标签PATENT_NAME、CUSTOMER_NAME等即是Word文档中的书签名

*/

$szValue = $aBookmarkItem[$Bookmark->Name];

if( !$szValue ) //替换书签中的值

$range->Text = trim($szValue);

}

$office->Quit();

?>

php操作word画表格实例代码:

$word = new COM("ation") or die("无法启动 Word 程序!");

$word->Visible = 1;

$doc = $word->Documents->Add();

$doc->Sections->Add($word->Selection->Range,0);// 增加一个分节

$Section = $doc->Sections(1); // 获取第一小节对象

$Range = $Section->Range; // 产生 Range 对象

$Table = $doc->Tables->Add($Range ,5, 10); // 产生 5x10的表格

// 将数据塞入表格

for ($i=1; $i<=10; $i++) {

for ($j=1; $j<=5; $j++) {

$Cell = $Table->Cell($j, $i);

$CellRange = $Cell->Range;

$CellRange->InsertBefore(chr(0x40+$j).chr(0x40+$i));

}

}

$word->Documents[1]->SaveAs("c:");

$word->Quit();

$word->Release();

$word = null;

?>

没有组件操作权限的解决方法:

1、运行

2、组件服务――计算机――我的电脑――DCOM配置――找到microsoft word 文档

3、点击属性

4、选择“安全性”

5、选定“使用自定义访问权限”和“使用自定义启动权限”

6、分别编辑权限,添加ASPNET,VS Developers,Debugger User

7、选择“身份标识”,在选定“交互式用户” 即可 (关键步骤

===============================================================================

$word = new COM("ation") or die ("Could not initialise MS Word object.");

$word->ActiveDocument->Open("");

// Extract content.

$content = (string) $word->ActiveDocument->Content;

echo $content;

$word->ActiveDocument->Close(false);

$word->Quit();

$word = null;

unset($word);

?>

=======================================================================================

生成excel电子表格

-PHP

include_once("");

$ex=new COM("") or die("Can't not open excel!");

//$ex->Application->Visible=1;

$wkb=$ex->application->workbooks->add();

$sheet=1;

excel_write_cell($wkb,$sheet,"A4","Hello,World!");

$exFileName=realpath(".")."".getGuid().".xls";

$wkb->SaveAs($exFileName);

$wkb->close();

$ex->application->Quit();

$ex=null;

function excel_write_cell($wkb,$sheet,$c,$v){

$sheets=$wkb->worksheets($sheet);

$sheets->activate;

$selcell=$sheets->Range($c);

$selcell->activate;

$selcell->value=$v;

}

function excel_read_cell($wkb,$sheet,$c){

$sheets=$wkb->worksheets($sheet);

$sheets->activate;

$selcell=$sheets->Range($c);

$selcell->activate;

return $selcell->value;

}

?>

使用word模板

-PHP

include_once("");

$tmpdoc=realpath("");

$customerinfo="Info Wyle COyote 123 Abc Ave. LooneyTune,USA 99999";

$deliverynum="00001";

$ordernum="12345";

$custnum="WB-beep";

$shipdate="11 Sep 2001";

$orderdate="11 Sep 2001";

$shipvia="UPS Ground";

$item[1]="SK-000-05";

$desc[1]="Acme Plcket Rocket";

$quantity[1]="2";

$cost[1]="$5.00";

$subtot[1]="$10.00";

$total="$10.00";

$word=new COM("ation") or die("Can't not start ms word!");

print "loaded word version {$word->Version}n
";

//$word->visible=1;

$word->Documents->open($tmpdoc);

$word->Application->Run("BkmkCustomer");

$word->Selection->TypeText($customerinfo);

$word->Application->Run("BkmkDelivery");

$word->Selection->TypeText($deliverynum);

$word->Application->Run("NextCell");

$word->Selection->TypeText($shipdate);

$word->Application->Run("NextCell");

$word->Selection->TypeText($shipvia);

$word->Application->Run("NextCell");

$word->Selection->TypeText($orderdate);

$word->Application->Run("NextCell");

$word->Selection->TypeText($custnum);

$word->Application->Run("NextCell");

$word->Selection->TypeText($ordernum);

//$word->Application->Run("NextCell");

$word->Application->Run("BkmkItem");

$word->Selection->TypeText($item[1]);

$word->Application->Run("NextCell");

$word->Selection->TypeText($desc[1]);

$word->Application->Run("NextCell");

$word->Selection->TypeText($quantity[1]);

$word->Application->Run("NextCell");

$word->Selection->TypeText($cost[1]);

$word->Application->Run("NextCell");

$word->Selection->TypeText($subtot[1]);

$word->Application->Run("BkmkTOtal");

$word->Selection->TypeText($total);

/*

//打印doc的代码

$word->("invoiceprint");//运行打印的doc宏

$word->Application->ActiveDocument->Saved=true;//保存

while($word->oundPrintingStatus>0)sleep)(1);//等待退出

*/

//下面是另存为的代码

$docFileName=realpath(".")."".getGuid().".doc";

$word->ActiveDocument->SaveAs($docFileName);

$word->quit();

//$word->Release();

$word=null;

//echo "生成doc完成!";

?>


本文标签: 操作 替换 文档