php中如何把html中的内容生成到word文档

1:在php中如何把html中的内容生成到word文档中
   2:php把html中的内容生成到word文档中时,不居中显示问题,即会默认按照web视图进行显示。
Word.class.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">
              <head>
                   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                   <xml><w:WordDocument><w:View>Print</w:View></xml>
            </head><body>';
    }
    function save($path)
    {


        echo "</body></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);
    }

}

index.php

/*生成doc文件*/
 $html = '<style>
.onle_bm{ width:50%; line-height:50px; font-size:24px; text-align:center; color:#000;margin: 0px auto;}
.onle_bts{ width:50%; line-height:30px; font-size:16px; color:#666; margin:0px auto;}
.onle_bd{ width:50%; margin:0px auto; padding-top:20px;}
.onle_bd table{ border-right: 1px solid #000;  border-bottom: 1px solid #000;height: 40px;}
.onle_bd td{font-size: 14px; color: #000; border-left: 1px solid #000;  border-top: 1px solid #000;height: 40px;}

</style><div class="onle_bm">国家登记表</div>
<div class="onle_bts">报名序号:<span>20170001</span></div>
<div class="onle_bd">
<form action="" method="get">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td width="14%" align="center"><p >报考岗位</p></td>
   <td width="34%"><label for="textfield"><p>'.$_POST['bkgw'].'</p></label>
     </td>
   <td width="11%" align="center"><p >专业类别</p></td>
   <td colspan="2">'.$_POST['sxzy'].'</td>
   <td width="18%" rowspan="4"><img src="'.$zhaopian.'" width="50px" height="100px" /></td>
 </tr>
 <tr>
   <td align="center"><p >姓名 </p></td>
   <td><p>'.$_POST['username'].'</p></td>
   <td align="center"><p >出生年月</p></td>
   <td colspan="2" align="center"><p >'.$_POST['csmy'].'</p></td>
   </tr>
 <tr>
   <td align="center"><p >户口所在地</p></td>
   <td><p >'.$_POST['hjaddress'].'</p></td>
   <td align="center"><p >性别</p></td>
   <td colspan="2" align="center"><p >'.$_POST['sex'].'</p></td>
   </tr>
 <tr>
   <td align="center"><p >政治面貌</p></td>
   <td align="center"><p >'.$_POST['zzmm'].'</p></td>
   <td align="center"><p >婚姻状况</p></td>
   <td colspan="2" align="center"><p >'.$_POST['hyzk'].'</p></td>
   </tr>
 <tr>
   <td align="center"><p >最高学历 </p></td>
   <td align="center"><p >'.$_POST['zgxl'].'</p></td>
   <td align="center"><p >毕业证号</p></td>
   <td width="11%" align="center"><p>'.$_POST['byzh'].'</p></td>
   <td width="12%" align="center"><p >学位</p></td>
   <td align="center"><p >'.$_POST['degree'].'</p></td>
 </tr>
 <tr>
   <td align="center"><p >毕业院校 </p></td>
   <td colspan="3"><p>'.$_POST['byyx'].'</p></td>
   <td align="center"><p >所学专业</p></td>
   <td align="center"><p >'.$_POST['sxzy'].'</p></td>
 </tr>
 <tr>
   <td align="center"><p >现工作单位</p></td>
   <td colspan="3"><p>'.$_POST['xgzdw'].'</p></td>
   <td align="center"><p >曾担任职务 </p></td>
   <td align="center"><p >'.$_POST['cdrzw'].'</p></td>
 </tr>
 <tr>
   <td align="center"><p >通讯地址</p></td>
   <td colspan="3"><p>'.$_POST['txaddress'].'</p></td>
   <td align="center"><p >邮政编码</p></td>
   <td align="center"><p >'.$_POST['yzbm'].'</p></td>
 </tr>
 <tr>
   <td align="center"><p >现居住地</p></td>
   <td colspan="3">'.$_POST['xjzaddress'].'</td>
   <td align="center"><p >移动电话</p></td>
   <td align="center"><p>'.$_POST['ydphone'].'</p></td>
 </tr>
 <tr>
   <td align="center"><p >身份证号</p></td>
   <td><p>'.$_POST['idcard'].'</p></td>
   <td align="center"><p >电子邮箱</p></td>
   <td colspan="3"><p>'.$_POST['email'].'</p></td>
   </tr>
 <tr>
   <td align="center"><p >掌握何种外语及程度</p></td>
   <td align="center"><p >'.$_POST['zwwycd'].'</p></td>
   <td align="center"><p >计算机掌握程度</p></td>
   <td align="center"><p >'.$_POST['jsjzwcd'].'</p></td>
   <td align="center"><p >有无刑事记录</p></td>
   <td align="center"><p >'.$_POST['xsfzjl'].'</p></td>
   </tr>
 <tr>
   <td align="center"><p >毕业年份</p></td>
   <td align="center"><p >'.$_POST['bynf'].'</p></td>
   <td colspan="3" align="center"><p >直系亲属是否从事商标代理</p></td>
   <td align="center"><p >'.$_POST['zxqscssbdl'].'</p></td>
 </tr>
 <tr>
   <td align="center"><p >学习经历 </p></td>
   <td colspan="5"><label for="textarea"></label><p>
    '.$_POST['xxjsxjl'].'</p></td>
   </tr>
 <tr>
   <td align="center"><p >工作经历 </p></td>
   <td colspan="5"><p>'.$_POST['gzjl'].'</p></td>
   </tr>
 <tr>
   <td align="center"><p >奖惩情况</p></td>
   <td colspan="5"><p>'.$_POST['jcqk'].'</p></td>
   </tr>
 <tr>
   <td rowspan="2" align="center"><p align="center" >家庭成员和 <br />
     主要社会 <br />
     关系 </p></td>
   <td align="center"><p >姓名 </p></td>
   <td align="center"><p >关系 </p></td>
   <td colspan="2" align="center"><p >所在单位职务 </p></td>
   <td align="center"><p >政治面貌 </p></td>
 </tr>
 <tr>
   <td colspan="5"></td>
   </tr>
 <tr>
   <td align="center"><p >备注</p></td>
   <td colspan="5">'.$_POST['bz'].'</td>
   </tr>
</table>


</form>
</div>
<div class="onle_bts">注:最高学历为国家承认的学历</div>
'; 


    $word = new word(); 
    $word->start(); 
    $wordname = '../asset/jianli/'.$newname.".doc";//生成文件路径
    echo $html; 
    $word->save($wordname); 
    ob_flush();//每次执行前刷新缓存 
    flush();
    /*生成doc文件 end*/

       针对第2个问题,下载到本地的word文档打开后显示默认按照web视图进行显示的问题:如下图:


     设置页面视图显示:


 
按照正常的页面视图进行显示的话,需要在头部添加一行xml标示进行设置(蓝色代码部分):<xml><w:WordDocument><w:View>Print</w:View></xml>,
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值