Home > Backend Development > PHP Tutorial > PHP completes the function of excel download through html-table form

PHP completes the function of excel download through html-table form

不言
Release: 2023-04-02 15:12:01
Original
1991 people have browsed it

This article mainly introduces the function of php to complete excel downloading in the form of html-table. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it

/** 
* @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);
}
Copy after login

The total of the export at the bottom is additional information

##The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP implements special effects of Chinese circular seal

PHP import progress bar class

How to download zip images using php

The above is the detailed content of PHP completes the function of excel download through html-table form. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template