PHP中将指定文本内容导入到word中

大家可能做导入导出excel的功能比较多,今天给大家看一下前2天刚做出来的将页面内容导入到word的方法。

 1 <?php
 2 //首先要添加一个类文件
 3 class word
 4 { 
 5     function start()
 6     {
 7         ob_start();
 8         echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
 9         xmlns:w="urn:schemas-microsoft-com:office:word"
10         xmlns="http://www.w3.org/TR/REC-html40">';
11     }
12     function save($path)
13     {
14  
15         echo "</html>";
16         $data = ob_get_contents();
17         ob_end_clean();
18  
19         $this->wirtefile ($path,$data);
20     }
21  
22     function wirtefile ($fn,$data)
23     {
24         $fp=fopen($fn,"wb");
25         fwrite($fp,$data);
26         fclose($fp);
27     }
28 }
29 //定义一个字符串
30 //$car=file_get_contents("http://www.baidu.com");
31 $car="
32 <table>
33    <tr>
34    <td>aaaa</td>
35    <td>bbbb</td>
36    </tr>
37    <tr>
38    <td>ccccc</td>
39    <td>ddddd</td>
40    </tr>
41 </table>
42 ";
43 $html = $car;
44  
45     $word = new word(); 
46     $word->start(); 
47     $wordname = "666.doc"; 
48     $wordname = iconv('UTF-8','GBK',   $wordname);//防止乱码
49     $html=iconv('UTF-8','GBK',  $html); //防止乱码
50     echo $html;
51     $word->save('C:/Users/Administrator/Desktop/'.$wordname); //可以自定义保存路径
52     ob_flush();//每次执行前刷新缓存 
53     flush();
54 ?>

 

posted @ 2018-01-24 14:00  大白驴  阅读(1313)  评论(0编辑  收藏  举报