`

php生成word文档的代码

    博客分类:
  • PHP
阅读更多

一、用windows里面自带的com,然后 php生成word文档

<?php

$word= new COM("word.application") or die("Unable to create Word document");
print "Loaded Word, version {$word->Version}\n";
$word->Visible = 0;
$word->Documents->Add();

//设置边距
$word->Selection->pageSetup->LeftMargin = '3';
$word->Selection->pageSetup->RightMargin = '3';

//设置字体
$word->Selection->Font->Name = 'Helvetica';

//设置字号
$word->Selection->Font->Size = 8;

//设置颜色
$word->Selection->Font->ColorIndex= 13; //wdDarkRed = 13
//输出到文档
$word->Selection->TypeText("string string string string string string ");
$range = $word->ActiveDocument->Range(0, 0);
$table_t = $word->ActiveDocument->Tables->Add($range,3,4);     //生成3行4列
$table_t->Cell(1, 2)->Range->InsertAfter('aaa');                              //向第1行第2列插入aaa
//保存
//$word->Sections->Add(1);
$word->Documents[1]->SaveAs(dirname(__FILE__)."/create_test.doc");

//退出
$word->quit();
?>

二、用php类直接生成word文档

<?php 

//生成word文档
// www.jbxue.com
class word 

function start() 

ob_start(); 
print’<html xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:w="urn:schemas-microsoft-com:office:word" 
xmlns="’">http://www.w3.org/TR/REC-html40">’; 
}

function save($path) 
{

print "</html>"; 
$data = ob_get_contents();

ob_end_clean();

$this->wirtefile ($path,$data); 
}

function wirtefile ($fn,$data) 
{

$fp=fopen($fn,"wb"); 
fwrite($fp,$data); 
fclose($fp); 


/*-------word class End-------*/ 
$word=new word; 
$word->start(); 
echo $cout; 
$wordname="word/".time().".doc"; 
$word->save($wordname);//保存word并且结束. 
?>

<?php
require_once("../../config/sys_config.php"); //配置文件
require_once("../../include/db_class.php");   
header("Content-type: text/html; charset=$page_Code"); //页面编码
header("Content-Type:application/msword");   
header("Content-Disposition:attachment;filename=".mb_convert_encoding("客户资料报表","GBK","$page_Code").".doc");   
header("Pragma:no-cache");
header("Expires:0");
$usersId = intval( $_GET['uid'] ); //用户ID
?>
<html>
<meta http-equiv=Content-Type content="text/html; charset=<?php echo $page_Code; ?>">
<style media="print" type="text/css">
<!--
body {
margin-left: 0cm;
margin-top: 0cm;
margin-right: 0cm;
margin-bottom: 0cm;
}
-->
</style>
<body>
<table width="100%" style='border-collapse:collapse;'>
<tr>
    <td width='24%' bgcolor='#CCCCCC' style='border:1px solid #000000;font-size:12px;'>客户名称</td>
    <td width='13%' bgcolor='#CCCCCC' style='border:1px solid #000000;font-size:12px;'>电话号码</td>
    <td width='32%' bgcolor="#CCCCCC" style='border:1px solid #000000;font-size:12px;'>客户地址</td>
    <td width='11%' bgcolor="#CCCCCC" style='border:1px solid #000000;font-size:12px;'>添加日期</td>
    <td width='20%' bgcolor="#CCCCCC" style='border:1px solid #000000;font-size:12px;'>客户备注备注</td>
</tr>
<?php
$sqlstr = "select * from clients where usersId=$usersId order by clientsId desc";
$rows   = $db -> select($sqlstr);
$num    = count($rows); //客户总数
for( $i = 0; $i < $num; $i++ )
{
?> 
<tr>
        <td style='border:1px solid #000000;font-size:12px;'><?php echo $rows[$i][clientsName]?></td>
        <td style='border:1px solid #000000;font-size:12px;'><?php echo $rows[$i][clientsPhone]?></td>
        <td style='border:1px solid #000000;font-size:12px;'><?php echo $rows[$i][clientsAddress]?></td>
        <td style='border:1px solid #000000;font-size:12px;'><?php echo $rows[$i][clientsTime]?></td>
    <td style='border:1px solid #000000;font-size:12px;'>&nbsp;</td>
   </tr>
<?php
}
?>
</table>
</body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics