php 生成 word 文档

 1 <?php 
 2 class word
 3 { 
 4     function start()
 5     {
 6         ob_start();
 7         echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
 8         xmlns:w="urn:schemas-microsoft-com:office:word"
 9         xmlns="http://www.w3.org/TR/REC-html40">';
10     }
11 
12     function save($path)
13     {
14         echo "</html>";
15         $data = ob_get_contents();
16         ob_end_clean();
17         $this->wirtefile ($path,$data);
18     }
19       
20     function wirtefile ($fn,$data)
21     {
22         $fp=fopen($fn,"wb");
23         fwrite($fp,$data);
24         fclose($fp);
25     }
26 }
27 
28 $html = ' 
29     <table width=600 cellpadding="6" cellspacing="1" bgcolor="#336699"> 
30     <tr bgcolor="White"> 
31      <td>baidu</td> 
32      <td><img src="http://mpic.tiankong.com/ecc/3e3/ecc3e349338dbe58603cf270d9cd7c9c/640.jpg?x-oss-process=image/resize,m_lfit,h_600,w_600/watermark,image_cXVhbmppbmcucG5n,t_90,g_ne,x_5,y_5"></td>
33      <td><a href="http://www.baidu.com" target="_blank" >http://www.baidu.com</a></td> 
34     </tr> 
35     <tr bgcolor="red"> 
36      <td>baidu</td> 
37      <td><a href="http://www.baidu.com" target="_blank" >http://www.baidu.com</a></td> 
38     </tr> 
39     <tr bgcolor="White"> 
40      <td colspan=2 > 
41      baidu<br> 
42      </td> 
43     </tr> 
44     </table> 
45 '; 
46 
47 //批量生成 
48 for($i=1;$i<=1;$i++){ 
49     $word = new word(); 
50     $word->start(); 
51     $wordname = 'TESTDOC-'.$i.".doc"; 
52     echo $html; 
53     $word->save($wordname); 
54     ob_flush();//每次执行前刷新缓存 
55     flush(); 
56 }
57 
58 
59 
60 
61 
62 
63 
64  
既不回头,何必不忘; 既然无缘,何须誓言; 今日种种,逝水无痕; 明夕何夕,君已陌路;
原文地址:https://www.cnblogs.com/zyjfire/p/7280033.html