<?php
/**
* 导出csv文件
* Created by PhpStorm.
* User: smile
* Date: 2019/5/14
* Time: 12:09
*/
class hlp_csv {
// "\t" 解决生成的csv样式问题
/**
* csv文件导出
* @param string $file
* @param array $head
* @param array $data
* @return bool
*/
public static function exportCsv(string $file, array $head, array $exportList) {
// 输出Excel文件头
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename=' . $file);
header('Cache-Control: max-age=0');
if (!file_exists($file)) {
$csv = fopen($file, 'a');
if (!$csv) {
return false;
}
//文件头
$headList = [];
foreach ($head as $key => $value) {
$headList[$key] = iconv('utf-8', 'gb2312', $value);
}
unset($key);
unset($value);
fputcsv($csv, $headList);
} else {
$csv = fopen($file, 'a');
if (!$csv) {
return false;
}
}
//文件内容
foreach ($exportList as $exportInfo ) {
foreach ($exportInfo as $key => $value) {
$exportInfo[$key] = iconv('utf-8', 'gb2312', $value);
}
fputcsv($csv, $exportInfo);
}
fclose($csv);
return true;
}
}
strval($inpourInfo['channel_order_no']) . "\t",
可避免大部分问题
php csv文件生成
于 2020-09-11 15:32:37 首次发布