美文网首页
导出数据到本地文件

导出数据到本地文件

作者: 洞房花猪 | 来源:发表于2016-04-18 14:29 被阅读183次

    把数据导出到本地的excel文件中的方法

    public static function export($rows, $coldefs, $boolPrintRows=true, $csvFileName=null, $separator=",",$title="")    
    {        
    $endLine = "\r\n";        
    $returnVal = "";         
    if($csvFileName != null)        
    {            
        header("Cache-Control: public");            
        header("Content-Description: File Transfer");            
        header("Content-Disposition: attachment; filename=".$csvFileName);            
        header("Content-Type: application/octet-stream");            
        header("Content-Transfer-Encoding: binary");       
    }        
    if($boolPrintRows == true)
    {            
        $names = '';            
        if(!empty($title)){                
            foreach($title as $col=>$config){                    
            $names .= $col.$separator;                
            }            
        } else {                
            foreach($coldefs as $col=>$config){                    
            $names .= $col.$separator;                
            }            
        }            
        $names = rtrim($names,$separator);            
        if($csvFileName != null){                
            echo iconv('UTF-8', 'GBK', $names.$endLine);          
            //echo $names.$endLine;            
        }else            
            $returnVal .= iconv('UTF-8', 'GBK', $names.$endLine);        
    }         
    foreach($rows as $row)
    {            
        $r = '';            
        foreach($coldefs as $col=>$config){                
            if(isset($row[$col])){                    
                $val = $row[$col];               
                $r .= $val.$separator;                
            }            
        }//       $r.= $val.$endLine;            
        $item = trim(rtrim($r,$separator)).$endLine;            
        if($csvFileName != null){                
            echo iconv('UTF-8', 'GBK', $item);            
        }else{                
            $returnVal .= iconv('UTF-8', 'GBK', $item);            
        }        
      }        
    return $returnVal;    
    }
    

    相关文章

      网友评论

          本文标题:导出数据到本地文件

          本文链接:https://www.haomeiwen.com/subject/rflalttx.html