php导出excle文件的封装

通过提交筛选条件,获取统计数据,然后以excel文件的格式导出来,这是一个比较常用的功能,最近恰好有用到,记录一下;

if ($export) { //导出操作
    include_once('../../semsys/include/export_class.php');
    二维数组,表示需要显示的数据
    $exceldata  = array();       
    $export_obj = new ExportFile('csv');
    $file_name  = "标签页中奖用户信息";
    $title      = "标签页中奖用户信息";
    $sub_title  = '';
    $note       = '';
    $key_list = array('wxuid', 'nickname', 'prize', 'phone', 'prize_time', 'is_check', 'check_time', 'is_send', 'send_time');
    $key_name = array('微信uid', '用户昵称', '奖品', '手机号' ,'中奖时间', '审核状态', '审核时间','是否发奖','发奖时间');
    $export_obj->export_csv($exceldata,$file_name,$key_list,$key_name,$title,$sub_title,$note);
    exit(0);
}

$key_list 表示数组下面的键,$key_name表示excel的表头,要一一对应;

执行这段代码,直接数输出文件;

导出类的封装

<?php
/**
 * 本文件为导出文件的基本类 可以根据查询的数据导出
 */
// 导出文件相关的类
class ExportFile
{
	// 文件名称
	private $file_name;
	// 文件后缀
	private $file_suffix;
	// 支持的文件格式
	private $support_suffix;
	// 导出文件的标题
	private $title;
	// 导出文件的副标题
	private $sub_title;
	// 导出文件的注释
	private $note;
	// 错误提示信息
	private $err_info;
	//导出数据的编码格式
	private $encode_type;
	// 表头
	// 文件后缀名 .text 包含'.'
	// 构造函数 $support_suffix 为文件导出的格式 后缀名 csv ,xlsx,txt
	function __construct($file_suffix,$encode_type = 'utf-8')
	{	
		$this->support_suffix = array('csv','xlsx','txt');
		if(in_array($file_suffix,$this->support_suffix))
		{
			$this->file_suffix = $file_suffix;
		}
		if(!$encode_type)
		{
			$this->encode_type = 'utf-8';
		}else
		{
			$this->encode_type = $encode_type;
		}
	}

	public function export_csv($data,$file_name = '',$key_list=array(),$key_name=array(),$title='',$sub_title='',$note='')
	{
		if(!$file_name)
		{
			$file_name = date('Ymd',time()).time().".".$this->file_suffix;
		}else
		{
			$file_name = $file_name.".".$this->file_suffix;
		}
		$this->file_name = $file_name;
		$result = $this->format_data($data,$key_list,$key_name,$title,$sub_title,$note);
		unset($data);
		$this->export($result,$this->file_name);
	}
	
	private function format_data($data,$key_list = array(),$key_name = array(),$title = '',$sub_title = '',$note = '')
	{
		if(!is_array($data)||empty($data))
		{
			echo '导出数据为空';
			exit();
		}
		$result = array();
		$result[0]['title'] = $title;
		$result[1]['sub_title'] = $sub_title;
		$result[2]['note'] = $note;
		$cn  = count($key_list);
		$re_cn = count($result);
		// 表头信息
		for($i = 0;$i < $cn;$i++)
		{
			$value = $key_name[$i];
			$value = preg_replace("/,/",',',$value);
			$value = preg_replace("/&gt;/",'>',$value);
			$value = preg_replace("/&lt;/",'<',$value);
			$value = preg_replace("/<br>/",'',$value);

			$result[$re_cn][$key_list[$i]] = $value;
			
		}

		$re_cn = count($result);
		foreach($data as $k=>$v)
		{
			for($i = 0;$i < $cn;$i++)
			{
				if($i == 0)
				{
					$result[$re_cn][$key_list[$i]] = $k;
				}else
				{
					$result[$re_cn][$key_list[$i]] = preg_replace("/,/",',',$v[$key_list[$i]]);
				}
			}
			$re_cn++;
		}
		return $result;
	}

	private function export($data,$filename)
	{
		if(!is_array($data)||empty($data))
		{
			echo '导出的数据为空';
			exit();
		}
		if($this->encode_type == 'utf-8')
		{
			$filename = iconv('utf-8','gbk',$filename);
		}
		
		ob_start();
		header('Pragma: public');
	    header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
	    header('Cache-Control: no-store, no-cache, must-revalidate');
	    header('Cache-Control: pre-check=0, post-check=0, max-age=0');
	    header('Content-Transfer-Encoding: binary');
	    header('Content-Encoding: utf-8');
	    header('Content-type: text/comma-separated-values');//文件类型
	    header('Content-Disposition: attachment; filename='.$filename.'');//文件名称
	 
	    foreach($data as $k=>$v)
	    {	
	    	$row = '';
	    	foreach($v as $m=>$n)
	    	{
	    		if($row!='' || $row ==0)
	    		{
	    			$row = $row.",".$n;
	    		}else
	    		{
	    			$row = $n;
	    		}
	    	}
	    	if($row == '')
	    	{
	    		continue;
	    	}
	    	$row.= "\r\n";
	    	if($this->encode_type == 'utf-8')
	    	{
	    		 $row = iconv('utf-8','gbk',$row);
	    	}
	    	
	    	echo $row;
	    }
	    ob_end_flush();
	    exit();
	}
};
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

runtoweb3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值