php教程生成word兩種方法
1.正常的touch創建word
2.fopen 打開word
3.fwrite 寫入word 並保存
這樣會出現一個問題 如果寫入的東西裡面含有html代碼的話,它將直接寫入word而不是 排版了
這個問題 需要在輸出html 代碼頭部加一段代碼
$headert='<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">';
$footer="</html>";
比如你的內容是$text;
那麼寫入的時候$text=$header.$text.$footer;
這樣的話fck裡面的東西就能按排版的樣式輸出了!
方法一
<?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->agesetup->rightmargin ='3"';
//設置字體 這
$word->selection->font->name ='helvetica';
//設置字號
$word->selection->font->size = 8;
//設置顏色
$word->selection->font->colorindex= 13; //wddarkred= 13
//輸出到文檔
$word->selection->typetext("hello world ");
$range = $word->activedocument->range(0,0);
$table_t =$word->activedocument->tables->add($range,3,4);
$table_t->cell(1,2)->range->insertafter('aaa');
//保存
//$word->sections->add(1);
$word->documents[1]->saveas(dirname(__file__)."/create_test.doc");
//退出
$word->quit();
?>
方法二
<?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=new word;
$word->start();
echo $cout;
$wordname="word/".time().".doc";
$word->save($wordname);//保存word並且結束