/*
*$name 文件名 string
*$title 表头 array
*$list 数据 array
*/
public static function excel($name,$title,$list)
{
$temp = '';
$filename = self::utfToGbk("{$name}.csv");
$fileData = self::utfToGbk(implode(",",$title)) . "\n";
if(count($list) == count($list,1)){
$keys = array_keys($list);
for ($i=0; $i<=count($keys)-1; $i++){
$temp .= $list[$keys[$i]] . ','.'' ;
}
$temp = substr($temp,0,-1);
$fileData .= self::utfToGbk($temp) . "\n";
}else{
foreach ($list as $key => $value) {
$temp = implode(",",$value);
$temp = str_replace("\n", "", $temp);
$fileData .= self::utfToGbk($temp) . "\n";
}
}
$filePath = __DIR__ . '/' . $filename;
// 将一个字符串写入文件
file_put_contents($filePath, $fileData);
unset($fileData);
$file=fopen($filePath,"r");
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($filePath));
header("Content-Disposition: attachment; filename={$name}.csv");
return fread($file,filesize($filePath));
}
public static function utfToGbk($data)
{
return iconv('utf-8', 'GBK//TRANSLIT//IGNORE', $data);
}
laravel 封装公共的导出方法
最新推荐文章于 2024-04-07 10:39:48 发布