opijoiu 发表于 2016-1-25 09:20:38

php将html转入word中

这是经过测试的,这种方法有一点不好,html页面代码要写在php中,不过好歹能运行,看程序
<?php
class word{
        function start(){
                ob_start();
                echo '<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)
        {
                echo "</html>";
                $data = ob_get_contents();
        }
        function wirtefile($fn,$data){
                $fp=fopen($fp,$data);
                fwrite($fp,$data);
        }
}

$html='
        <table width=600 cellpadding="6" cellspacing="1" style="border:1px solid green;" style="border-collapse:collapse">
        <tr style="border:1px solid green;">
<td style="border:1px solid green;">姓名</td>
<td style="border:1px solid green;">性别</td>
<td style="border:1px solid green;">年龄</td>
<td style="border:1px solid green;">爱好</td>
<td style="border:1px solid green;">备注</td>
        </tr>
        <tr style="border:1px solid green;">
          <td style="border:1px solid green;">张三</td>
          <td style="border:1px solid green;">男</td>
          <td style="border:1px solid green;">32</td>
          <td style="border:1px solid green;">足球</td>
          <td style="border:1px solid green;">无</td>
        </tr>
        <tr style="border:1px solid green;">
          <td style="border:1px solid green;">
                  李四
          </td>
          <td style="border:1px solid green;">男</td><td style="border:1px solid green;">43</td><td style="border:1px solid green;">篮球</td><td style="border:1px solid green;">无</td>
        </tr>
        </table>
';
//批量生成
//for($i=1;$i<3;$i++){
        $word = new word();
        $word->start();
        $wordname='个人测试php生成word.doc';
        echo $html;
       
        $word->save($wordname);
        header('Content-type:application/word');
        header('Content-Disposition: attachment; filename='.$wordname.'');
        //readfile($wordname);
        ob_flush();//每次执行前刷新缓存
        flush();

//}
?>


页: [1]
查看完整版本: php将html转入word中