php產生word兩種方法_PHP教程

來源:互聯網
上載者:User
php教程產生word兩種方法

1.正常的touch建立word

2.fopen 開啟word

3.fwrite 寫入word 並儲存

這樣會出現一個問題 如果寫入的東西裡面含有html代碼的話,它將直接寫入word而不是 排版了

這個問題 需要在輸出html 代碼頭部加一段代碼

$headert=' xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';
$footer="";

比如你的內容是$text;

那麼寫入的時候$text=$header.$text.$footer;

這樣的話fck裡面的東西就能按排版的樣式輸出了!


方法一

$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();

?>

方法二

class word
{
function start()
{
ob_start();
print'xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';
}
function save($path)
{
print "";
$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並且結束


http://www.bkjia.com/PHPjc/444807.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444807.htmlTechArticlephp教程產生word兩種方法 1.正常的touch建立word 2.fopen 開啟word 3.fwrite 寫入word 並儲存 這樣會出現一個問題 如果寫入的東西裡面含有html代碼的...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.