<?php
namespace app\admin\controller;
use think\Db;
class Exle extends Common
{
//$header 标头 例:['序号','姓名','性别','年龄'];
//$list 数据列表 例:[[1,2,3,4],[1,2,3,4]]
//$exleName 表格名称:例:2022-11-10
public function put($header,$list,$exleName){
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$exleName.".csv");//导出文件名
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "w");
$converter = function($value) {
return iconv('utf-8', 'gbk', $value);
};
$header = array_map($converter, $header);
fputcsv($output, $header);
//填充
foreach($list as $k => $v){
$csvrow = array_map($converter, $v);
fputcsv($output, $csvrow);
}
fclose($output);
}
//调用测试
public function index(){
$header = ['序号','姓名','性别','年龄'];
$list = [[1,2,3,4],[1,2,3,4]];
$exleName = date('Ymd');
$this->put($header,$list,$exleName);
}
}
