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->font->name ='helvetica';
-
- $word->selection->font->size = 8;
-
- $word->selection->font->colorindex= 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->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);