美文网首页
php 通过html-table形式完成excel下载的功能实现

php 通过html-table形式完成excel下载的功能实现

作者: Mracale | 来源:发表于2023-08-28 15:11 被阅读0次
    /** 
    * @param $titles 列名 
    * @param $datas  具体数据 
    * @param $filename 文件名 
    * @param $extraMessage 额外信息 
    */
    public static function createExcel($titles,$datas, $filename, $extraMessage = ''){    
    $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";    
    $str .= "<table border=1>";    
    //表头    
    $str .= "<tr>";    foreach ($titles as $title)
     {        
    $str .= "<td style='width: 60px;'>{$title}</tdw>";    
    }    
    $str .= "</tr>\n";    
    //具体数据    
    foreach ($datas as $key => $rt)
     {        
    $str .= "<tr>";        
    foreach ($rt as $k => $v)
     {            
    $str .= "<td>{$v}</td>";        
    }        
    $str .= "</tr>\n";    
    }    
    //额外信息    
    if (!empty($extraMessage))
     {        
    $str .= "<tr style='height:50px;border-style:none;'><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='19' >{$extraMessage}</th></tr>";    
    }    
    $str .= "</table></body></html>";    
    header("Content-Type: application/vnd.ms-excel; name='excel'");    
    header("Content-type: application/octet-stream");    
    header("Content-Disposition: attachment; filename=" . $filename);    
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");    
    header("Content-Type:application/download");;    
    header("Pragma: no-cache");    
    header("Expires: 0");    
    exit($str);
    }
    

    相关文章

      网友评论

          本文标题:php 通过html-table形式完成excel下载的功能实现

          本文链接:https://www.haomeiwen.com/subject/pivgmdtx.html