首頁  >  文章  >  後端開發  >  php 透過html-table形式完成excel下載的功能實現

php 透過html-table形式完成excel下載的功能實現

不言
不言原創
2018-07-05 16:11:201908瀏覽

這篇文章主要介紹了關於php 透過html-table形式完成excel下載的功能實現,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

/** 
* @param $datas  具体数据 
* @param $titles 列名 
* @param $filename 文件名 
* @param $extraMessage 额外信息 
*/
public static function createExcel($datas, $titles, $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=&#39;width: 60px;&#39;>{$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=&#39;height:50px;border-style:none;&#39;><th border=\"0\" style=&#39;height:60px;width:270px;font-size:22px;&#39; colspan=&#39;19&#39; >{$extraMessage}</th></tr>";    
}    
$str .= "</table></body></html>";    
header("Content-Type: application/vnd.ms-excel; name=&#39;excel&#39;");    
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中文網!

相關推薦:

PHP實作中文圓形印章的特效

##PHP導入進度條類別

php打包zip圖片下載的方法

以上是php 透過html-table形式完成excel下載的功能實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn