1.正常的touch創(chuàng)建word 2.fopen 打開word 3.fwrite 寫入word 并保存
這樣會出現(xiàn)一個(gè)問題,如果寫入的東西里面含有html代碼的話,它將直接寫入word而不是 排版了.
這個(gè)問題,需要在輸出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>";
比如你的內(nèi)容是$text;
那么寫入的時(shí)候$text=$header.$text.$footer;
這樣的話fck里面的東西就能按排版的樣式輸出了!
方法一,實(shí)例代碼如下:
- <?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();
- //設(shè)置邊距 這個(gè)有錯(cuò)誤
- // $word->selection->agesetup->rightmargin ='3"';
- //設(shè)置字體 這
- $word->selection->font->name ='helvetica';
- //設(shè)置字號
- $word->selection->font->size = 8;
- //設(shè)置顏色
- $word->selection->font->colorindex= 13; //wddarkred= 13
- //輸出到文檔
- $word->selection->typetext("hello world ");
- //開源代碼Vevb.com
- $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();
- ?>
方法二,實(shí)例代碼如下:
- <?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);
- }
- }
- ?>
調(diào)用方法,代碼如下:
- $word=new word;
- $word->start();
- echo $cout;
- $wordname="word/".time().".doc";
- $word->save($wordname);//保存word并且結(jié)束
新聞熱點(diǎn)
疑難解答