//处理csv
$fileName = "拼团订单明细";
$header = [
'拼团主单号',
'拼团子单号',
'子订单号',
'支付时间',
'交易流水号',
'商品SKU',
'商品名称',
'规格',
'数量',
'金额',
'下单用户id',
'下单用户昵称',
'团长id',
'团长昵称',
'拼团订单状态',
'销售订单状态',
]; //表头信息
$fields = [
'groupon_no',
'groupon_child_no',
'parcel_number',
'pay_time',
'pay_transaction_id',
'sku_id',
'good_name',
'specification',
'buy_goods_num',
'pay_amt_e2',
'buyer_id',
'buyer_name',
'groupon_author_id',
'groupon_author_name',
'buyer_status',
'sell_order_status',
];
//设置header
$header = array_combine($fields,$header);
BaseFormModel::csvExport($data, $header, [], $fileName, true, 'utf-8');
/**
* 导出csv
* @param array $data 数据
* @param array $headers csv标题+数据
* @param array $specHeaders 需要转成字符串的数组下标
* @param string $fileName 文件名称
* @param bool $isFirst 是否只去第一条
* @param string $fontType 需要导出的字符集 csv默认为utf-8
* @author zhaohao
* @date 2019-12-10 11:38
*/
public static function csvExport(array $data, array $headers, $specHeaders = [], $fileName = '', $isFirst = false, $fontType = 'gbk//IGNORE')
{
//终端导出无需header头
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
header('Cache-Control: max-age=0');
$fp = fopen('php://output', 'a');
foreach ($headers as $key => $value) {
$headers[$key] = mb_convert_encoding($value, $fontType, 'utf-8');
}
if ($isFirst) {
fputcsv($fp, $headers);
}
//计数器
$num = 0;
$limit = 50000;
//逐行取出数据,不浪费内存
$count = count($data);
for ($i = 0; $i < $count; $i++) {
$num++;
if ($limit % 200 == $num) {
ob_flush();
flush();
$num = 0;
}
$row = $data[$i];
$ret = [];
foreach ($headers as $key => $value) {
if (!empty($specHeaders) && in_array($key, $specHeaders)) {
$ret[$key] = mb_convert_encoding($row[$key], $fontType, 'utf-8') . "t";
} else {
$ret[$key] = mb_convert_encoding($row[$key], $fontType, 'utf-8');
}
}
fputcsv($fp, $ret);
}
unset($data);
unset($ret);
fclose($fp);
exit;
}