首页 > Web开发 > 详细

htmlTableToExcel

时间:2021-09-07 16:05:34      阅读:33      评论:0      收藏:0      [点我收藏+]
    //导出excel
    public function dataToExcel($filename, $data, $header, $keys)
    {
        $html = $this->dataToHtml($data, $header, $keys);
        return $this->htmlToExcel($filename, $html);
    }

    public function dataToHtml($data, $header, $keys)
    {
        $str = "<html><head><meta charset=\"utf-8\"></head><body>";
        $str .= "<table border=\"1\"><tr>";
        foreach ($header as $v) {
            $str .= "<th>{$v}</th>";
        }
        $str .= "</tr>";
        foreach ($data as $row) {
            $str .= "<tr>";
            foreach ($keys as $v) {
                $str .= "<td>{$row[$v]}</td>";
            }
            $str .= "</tr>";
        }
        $str .= "</table><body></html>";
        return $str;
    }

    public function htmlToExcel($filename, $html)
    {
        header("Content-Type: application/vnd.ms-excel;charset=utf-8; name=‘excel‘");
        // header(‘Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);
        // header( "Content-type: application/octet-stream" );
        header("Content-Disposition: attachment; filename=" . $filename . ‘.xls‘);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header("Expires: 0");
        exit($html);
    }

 

htmlTableToExcel

原文:https://www.cnblogs.com/CanyingV/p/15236668.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!