将数据输出到Excel表格中的方法

1.第一种
<?php
	$str = '';
	$str = '<table border = "2">
				<thead>
					<tr>
						<th>顶顶顶顶顶顶顶顶顶顶顶顶顶顶</th>
						<th>2</th>
						<th>3</th>
						<th>3</th>						
					</tr>
				</thead>
				<thead>
					<tr>
						<th>1</th>
						<th>2</th>
						<th>3</th>
						<th>3</th>						
					</tr>
				</thead>				
	';
	$filename = '11111111';
	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:filename=".$filename.".xls");
	echo $str;
?>

2.第二种,封装为一个类

<?php
/**
 * @copyright 2014 SYSTEM_77NETADMIN
 * @description: 输出为execl类
 * @file: cls_output_excl.php
 * @author: Liu
 * @charset: UTF-8
 * @time: 2014-09-01 14:07:37
 * @version 1.0.0
**/
class cls_output_execl {
    public $inEncode; //页面编码
    public $outEncode; //Excel文件的编码

    public function __construct(){
      
    }
    /**
     * @name: set_encode
     * @description: 设置编码
     * @scope: public
     * @param: string 页面编码
     * @param: string Excel文件的编码
     * @return: mixed
     * @author: Liu 
     * @create: 2014-09-01 14:07:37
    **/
    public function set_encode($incode,$outcode){
        $this->inEncode=$incode;

        $this->outEncode=$outcode;
    }
    /**
     * @name: set_title
     * @description: 设置Execl标题栏
     * @scope: public
     * @param: array 
     * @return: array
     * @author: Liu  
     * @create: 2014-09-01 14:07:37
    **/
    public function set_title($titlearr){
        $title="";
        foreach($titlearr as $v){
            if($this->inEncode!=$this->outEncode){
                $title.=iconv($this->inEncode,$this->outEncode,$v)."\t";
            }
            else{
                $title.=$v."\t";
            }
        }
        $title.="\n";
        return $title;
    }

    /**
     * @name: set_title
     * @description: 设置Execl内容
     * @scope: public
     * @param: array 
     * @return: array
     * @author: Liu
     * @create: 2014-09-01 14:07:37
    **/
    public function set_row($array){
        $content="";
        foreach($array as $k => $v){
            foreach($v as $vs){
                if($this->inEncode!=$this->outEncode){
                    $content.=iconv($this->inEncode,$this->outEncode,$vs)."\t";
                }
                else{
                    $content.=$vs."\t";
                }
            }
            $content.="\n";
        }
        return $content;
    }
    
    /**
     * @name: set_title
     * @description: 生成并自动下载Excel,文件名称 (为空,已当前日期为名称)
     * @scope: public
     * @param: array 
     * @return: mixed
     * @author: Liu 
     * @create: 2014-09-01 14:07:37
    **/    
    public function get_excel($titlearr,$array,$filename=''){
        if($filename==''){
            $filename=date('YmdHis');
        }
        $title=$this->set_title($titlearr);
        $content=$this->set_row($array);
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:filename=".$filename.".xls");
        echo $title;
        echo $content;
        die;
    }
}

调用方法:

//输出到execl
if($output_op == 'output'){ 
    g('output_execl') -> set_encode('utf-8','utf-8'); 
    $title = array('消息序号','商家名称','商铺名称','消息标题','消息发送时间','发送的人数');
    $contentarr = $data['data'];
    if(is_array($contentarr) && count($contentarr)){
        foreach ($contentarr as $key => &$val){
            unset($val['ms_desc']);
            unset($val['ms_issend']);
            unset($val['pus_read']);
            unset($val['bui_call']);
            if($val['ms_send_time']){
                $contentarr[$key]['ms_send_time'] = date('Y-m-d H:i:s',$val['ms_send_time']);
            }
        }
    } 
        
     g('output_execl') -> get_excel($title,$contentarr);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值