word导出数据(tp5)
你好!为完成各种业务的需求,在我们日常开发中,多少会应要客户的要求,导出word文档,今天我们学习一下导出word文档,导出word非常简单只需要一个方法就可以了。
只需要一个方法
public function word(){
$ids = $_POST['ids'];//可以在这里用Ajax接收列表传过来的ID进行查询
//在第二个tr这里只需要把数据集查询出来在这里便利就可以。
$content = '<h4>表头:</h4>
<table border="1">
<tr>
<th>姓名</th>
<th>电话</th>
<th>电话</th>
</tr>
<tr>
<td>在这里遍历我</td>
<td>在这里遍历我</td>
<td>在这里遍历我</td>
</tr>
</table>';
$fileName='new_file.doc';
if(empty($content)){
return;
}
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$fileName");
$html = '<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">';
$html .= '<head><meta charset="UTF-8" /></head>';
echo $html . '<body>'.$content .'</body></html>';
}