PHP批量导出数据为excel表格

时间:2023-03-09 18:47:07
PHP批量导出数据为excel表格

之前用插件phoexcel写过批量导入数据,现在用到了批量导出,就记录一下,这次批量导出没用插件,是写出一个表格,直接输出

//$teacherList 是从数据库查出来的二维数组  $execlname是要导出的文件名
if(is_array($teacherList)){
foreach($teacherList as $k=>$val){
$strTable .= '<tr style="text-align:center;font-size:12px;">';
$strTable .= '<td >'.$val['username'].'</td>';
$strTable .= '<td >'.$val['school'].'</td>';
$strTable .= '<td >'.$val['tel'].'</td>';
$strTable .= '<td >'.$val['email'].'</td>';
$strTable .= '<td >'.$val['teacher_name'].'</td>';
$strTable .= '<td >'.$val['create_time'].'</td>';
$strTable .= '<td >'.$val['status'].'</td>';
$strTable .= '</tr>';
}
//释放$teacherList
unset($teacherList);
}
$strTable .='</table>';
//excel表格名 对中文转码
$filename = iconv("utf-8", "GB2312", $excelname);
//导出表格 strTable表格内容 filename表格名
header("Content-type: application/vnd.ms-excel");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment;filename="'.$excelname.'.xlsx"');
header('Expires:0');
header('Pragma:public');
echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
exit();
}

这个是之前写的批量导入表格数据到数据库 使用PHPExcel实现数据批量导入到数据库