=波波日志 > PHP/apache/Perl > php学习--使用COM=

php学习--使用COM

示例代码点击这里下载


生成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]);
$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完成!";
?>
类别:PHP/apache/Perl 作者:波波 日期:2009-07-10 【评论:0】 
 
暂时没有评论!
发表留言
  • *昵称:
  • 头像:
  • 电子邮件: [留下您的邮件,方便管理员回复您。]
  • 个人网站: *验证码:
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载或引用的作品侵犯了您的权利,请通知我们,我们会及时删除!
Powered by showbo,©2012WEB编程网桂ICP备05005887号 京公网安备1101055090