word导出数据(tp5)

word导出数据(tp5)

你好!为完成各种业务的需求,在我们日常开发中,多少会应要客户的要求,导出word文档,今天我们学习一下导出word文档,导出word非常简单只需要一个方法就可以了。

只需要一个方法

public function word(){
	$ids = $_POST['ids'];//可以在这里用Ajax接收列表传过来的ID进行查询
		
		//在第二个tr这里只需要把数据集查询出来在这里便利就可以。
   $content = '<h4>表头:</h4>
              <table border="1">
              <tr>
                <th>姓名</th>
                <th>电话</th>
                <th>电话</th>
              </tr>
              <tr>   
                <td>在这里遍历我</td>
                <td>在这里遍历我</td>
                <td>在这里遍历我</td>
              </tr>
              </table>'; 
               
   $fileName='new_file.doc';

    if(empty($content)){
        return;
    }

    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=$fileName");

    $html = '<html xmlns:v="urn:schemas-microsoft-com:vml"
         xmlns:o="urn:schemas-microsoft-com:office:office"
         xmlns:w="urn:schemas-microsoft-com:office:word" 
         xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" 
         xmlns="http://www.w3.org/TR/REC-html40">';
    $html .= '<head><meta charset="UTF-8" /></head>';

    echo $html . '<body>'.$content .'</body></html>';

  }

直接访问这个方法就可以了。查询完数据之后会自动输出到页面Word 文档下载下来即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值