您的位置:首页 > 编程语言 > PHP开发

PHP生成WORD

2011-04-14 10:59 274 查看
word.class.pho

 

<?php
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.php

<?php
include("word.class.php");
header("Content-type: text/html; charset=utf-8");
$word=new word;

$word->start();
$word_array =array('表头','表头','内容','内容');
?>
<table>
<tr>
<th><?php echo iconv("UTF-8","GB2312",$word_array[0]);?></th>
<th><?php echo iconv("UTF-8","GB2312",$word_array[1]);?></th>
</tr>
<tr>
<td><?php echo iconv("UTF-8","GB2312",$word_array[2]);?></td>
<td><?php echo iconv("UTF-8","GB2312",$word_array[3]);?></td>
</tr>
</table>

<?
$word->save("data.doc");//保存word并且结束.

echo '
<title>直接用php创建word文档</title>
<a href="data.doc" mce_href="data.doc" target=_blank>下载doc</a>';

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: