使用php程序,直接输出html格式的table表格代码,设置输出头,即可实现数据导出为excel文件:
$table = '<meta http-equiv=Content-Type content="text/html; charset=utf-8">';
$table .= "<table>
<thead>
<tr>
<th>A1单元格</th>
<th>B1单元格</th>
<th>C1单元格</th>
<th>D1单元格</th>
<th>E1单元格</th>
<th>F1单元格</th>
</tr>
</thead>
<tbody>";
$data = [
[
'data1' => 'value1',
'data2' => 'value2',
'data3' => 'value3',
'data4' => 'value4',
'data5' => 'value5',
'data6' => 'value6'
]
];
foreach($data as $item) {
$table .= "
<tr>
<td>{$item['data1']}</td>
<td>{$item['data2']}</td>
<td>{$item['data3']}</td>
<td>{$item['data4']}</td>
<td>{$item['data5']}</td>
<td>{$item['data6']}</td>
</tr>";
}
$table .= "</tbody></table>";
header('Content-type: text/html; charset=utf-8');
header('Content-type:application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: filename="export.xls"'); // 文件名自己改
echo $table;
关键词:phpexcel
所属分类:网站技术
本文地址:https://me.i-i.me/article/46.html
评论