thinkphp中通過一週 一個月 三個月 半年 自定義時間段導出 csv

class ExcelAction extends Action {
        
    public function index(){
        
        $this->export_csv();
    
    }
    function export_csv() {
    $filename = date('YmdHis').".csv";
    
    header("Content-type:text/csv");
    
    header("Content-Type: application/vnd.ms-excel; charset=UTF-8");  
    
    header("Content-Disposition:attachment;filename=".$filename);
    
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    
    header('Expires:0');
    
    header('Pragma:public');
           echo    $this->array_to_string($this->get_export_data());
    
    }
    
        //excel中顯示的數據
    function array_to_string($result) {
        
        if(empty($result)) {
            
            return "沒有符合您要求的數據!^_^";
        }
         $data = $this->strinput('客戶姓名,聯繫電話,送貨地址,電子郵箱,留言信息'."\r\n");
        foreach ($result as $v){

            $data .= $this->strinput($v['name'].','.$v['tel'].','.$v['address'].','.$v['zipcode'].','.$v['message']."\r\n");
        
        }    
        return $data;
    }
    
    //查詢數據
    function get_export_data() {
        
 
                $Dao = M('orders');
                
                //獲取當前月有多少天
                $moth = date('t');
                
                var_dump(strtotime(2014-09-07));
                //一週內數據
                if($_GET['monthtime']==1){
                    
                    $time =time()-604800;
                
                    $where = 'createtime >'.$time;
                
                }
                //一個月內數據    
                elseif($_GET['monthtime']==2){
                    if($moth ==28){
                        $time =time()-2419200;
                        
                        $where = 'createtime >'.$time;
                    }
                    if($moth ==29){
                        $time =time()-2505600;
                        
                        $where = 'createtime >'.$time;
                    }
                    if($moth ==30){
                        $time =time()-2592000;
                        
                        $where = 'createtime >'.$time;
                    }
                    if($moth ==31){
                        $time =time()-2678400;
                        
                        $where = 'createtime >'.$time;
                    }    
                
                }
                //三個月內的數據
                elseif($_GET['monthtime']==3){
                    
//                        $starttime=mktime(0,0,0,date("m")-3,1,date("Y"));
//                        $time = (date('m',time())-$starttime)*24*60*60;
                        
                        $time = time()-    7776000;
                            
                            
                        $where = 'createtime>'.$time;
                
                }
                
                //半年內的數據
                elseif ($_GET['monthtime']==4){
                
//                        $starttime=mktime(0,0,0,date("m")-6,1,date("Y"));
//                        $time = (date('m',time())-$starttime)*24*60*60;
                        
                        $time = time()-    15552000;
                            
                        $where = 'createtime>'.$time;
                
                }
                
                //自定義時間段
                elseif ($_GET['monthtime']==5){
                    
                    $begintime = strtotime($_GET['begintime']);
                    
                    $lasttime = strtotime($_GET['lasttime']);
                    
                    $where = "createtime between ' ".$begintime."'and ' ".$lasttime." ' ";
                    
                }
                
                $res = $Dao->where($where)->select();
                
                return $res;
    }
    
    function strinput($strInput) {
//        return $strInput;
        return iconv("UTF-8","GB2312",$strInput) ;//頁面編碼爲utf-8時使用,否則導出的中文爲亂碼
    }
}

====================================================================================================

上面是ExcelAction方法

下面是頁面中導出和單選按鈕

====================================================================================================

頁面導出按鈕

<form action ="__APP__/Excel/index" name="form2">
        <input type="radio" name="monthtime" id="export_time" value =1 checked="checked" />一週內
        <input type="radio" name="monthtime" id="export_oy" value =2 />一個月內
        <input type="radio" name="monthtime" id="export_ty" value =3 />三個月內
        <input type="radio" name="monthtime" id="export_yy" value =4 />半年內
        <input type="radio" name="monthtime" id = "custom"  value =5 />自定義
        <input class="Wdate" type="text"  name="begintime"  id="begintime"  onClick="WdatePicker()"> <span id="spand" style=display:none >到</span>
        <input class="Wdate" type="text" name="lasttime"   id="lasttime" onClick="WdatePicker()">
        <input type='submit' name ="sub"     id="sub" value='導出'/>
    </form>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章