如何利用PHP操作Word文档
如何利用PHP操作Word文档,比如生成、修改后保存、删除等等……
答:用COM函数操作 MS Word
<?php
#实例化一个对象
$word = new COM("word.application") or die("Unable to instantiate Word");

#取得并显示版本
print "Loaded Word, version {$word->Version}<BR>";

#另一种方法去取得版本
$testversion = com_get($word->application,version);
print "Version using Com_get(): $testversion <BR>";

#使其可见
$word->Visible = 1;

#创建新文件
$word->Documents->Add();

#写字符
$word->Selection->TypeText("This is ");

#保存
$word->Documents[1]->SaveAs("Useless test.doc");

#关闭
$word->Quit();

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

如下为一部分操作WordPHP代码:
/*
* 创建日期:
* :
* :
* :
* : $Revision$
* $Log$
*/


//实例化一个world对象
$office = new COM("word.application") or die("Unable to instantiate Word");

if( ! $office )
showError(0, "Office 操作错误",true);

//调用Word显示文档
$office->Visible = 1;

$szFile = "d:/doc/patent1.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_NAMECUSTOMER_NAME等即是Word文档中的书签名
*/
$szValue = $aBookmarkItem[$Bookmark->Name];

if( !$szValue ) //替换书签中的值
$range->Text = trim($szValue);
}

$office->Quit();

?>
php操作word画表格实例代码
<?php
$word = new COM("word.application") 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.doc");
$word->Quit();
$word->Release();
$word = null;
?>
没有组件操作权限的解决方法:
1、运行
2、组件服务――计算机――我的电脑――DCOM配置――microsoft word 文档
3、点击属性
4、选择安全性
5、选定使用自定义访问权限使用自定义启动权限
6、分别编辑权限,添加ASPNET,VS Developers,Debugger User
7、选择身份标识,在选定交互式用户即可 (关键步骤
===============================================================================
<?php
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->ActiveDocument->Open("doc1.doc");
/
/ Extract content.
$content = (string) $word->ActiveDocument->Content;
echo $content;
$word->ActiveDocument->Close(false);
$word->Quit();
$word = null;
unset($word);
?>
=======================================================================================
生成excel电子表格
+展开
-PHP
<?php
include_once("guid.php");

$ex=new COM("Excel.sheet"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
<?php

include_once("guid.php");

$tmpdoc=realpath("temple.doc");

$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("word.application"or die("Can't not start ms word!");
print "loaded word version {$word->Version}\n<br/>";
//$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]);
php实例代码大全$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->Application.Run("invoiceprint");//运行打印的doc
$word->Application->ActiveDocument->Saved=true;//保存
while($word->Application.BackgroundPrintingStatus>0)sleep)(1);//等待退出
*/

//下面是另存为的代码
$docFileName=realpath(".")."\\".getGuid().".doc";
$word->ActiveDocument->SaveAs($docFileName);
$word->quit();
/
/$word->Release();
$word=null;
//echo "生成doc完成!";
?>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。