From 10b6c0758b0dbdb8b3bb1b0c5ebdf85f615ca537 Mon Sep 17 00:00:00 2001 From: gongfuxiang Date: Wed, 22 Jun 2022 22:14:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ecsv=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/base/Excel.php | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/extend/base/Excel.php b/extend/base/Excel.php index 8a8b2d870..bda7974a4 100755 --- a/extend/base/Excel.php +++ b/extend/base/Excel.php @@ -80,6 +80,53 @@ class Excel $this->warap_text = isset($params['warap_text']) ? intval($params['warap_text']) : 1; } + /** + * 导出CSV文件 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2022-06-22 + * @desc description + */ + public function ExportCsv() + { + // 获取配置编码类型 + $excel_charset = MyC('admin_excel_charset', 0); + $charset = MyConst('common_excel_charset_list')[$excel_charset]['value']; + + // 拼接文件信息,这里注意两点 + // 1、字段与字段之间用逗号分隔开 + // 2、行与行之间需要换行符 + // 3、英文逗号替换未中文逗号、避免与csv分隔符冲突 + $csv_title = implode(',', array_map(function($v) { + return str_replace([',', "\n"], [',', ''], $v['name']); + }, $this->title)); + $csv_content = (($excel_charset == 0) ? $csv_title : iconv('utf-8', $charset, $csv_title))."\n"; + foreach($this->data as $v) + { + $temp = ''; + $index = 0; + foreach($this->title as $tk=>$tv) + { + if(array_key_exists($tk, $v)) + { + $temp .= ($index == 0 ? '' : ',').str_replace([',', "\n"], [ ',', ''], $v[$tk]); + } + $index++; + } + $csv_content .= (($excel_charset == 0) ? $temp : iconv('utf-8', $charset, $temp))."\n"; + } + + // 头信息设置 + header("Content-type:text/csv"); + header("Content-Disposition:attachment;filename=" . $this->filename.'.csv'); + header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); + header('Expires:0'); + header('Pragma:public'); + echo $csv_content; + exit; + } + /** * 根据字段个数,设置表头排序字母 * @author Devil