导出数据到本地文件

把数据导出到本地的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;    
}

你可能感兴趣的:(导出数据到本地文件)