当前位置:Gxlcms > PHP教程 > php导出中文内容excel文件类实例_php技巧

php导出中文内容excel文件类实例_php技巧

时间:2021-07-01 10:21:17 帮助过:23人阅读

本文实例讲述了php导出中文内容excel文件类。分享给大家供大家参考。具体如下:

<?php 
class toExcel{ 
 public $link = null; 
 function __construct(){ 
 }  
 /*************************************************************************** 
 * $mapping:数组格式头信息$map=array('No','Name','Email','Age'); 
 * $datalist:数据库查出来的结果集 
 * $fileName:Excel文件名称 
 * return:Excel格式文件 
 **************************************************************************/ 
 public function toExcel($mapping,$datalist,$fileName) { 
  header("Content-type:application/vnd.ms-excel"); 
  header("Content-Disposition:filename=".iconv('utf-8', 'gb2312', $fileName).".xls"); 
  echo' 
   
   
   
   
   
  '; 
  echo''; 
  echo''; 
  if(is_array($mapping)) { 
   foreach($mapping as $key=>$val) 
   echo""; 
  } 
  echo''; 
  foreach($datalist as $k=>$v){ 
   echo''; 
   foreach($v as $key=>$val){ 
    if(is_numeric($val) && strlen($val)>=14){ 
     echo""; //大于14位的数字转换成字符串输出(如身份证) 
    }else{ 
     echo""; 
    } 
   } 
   echo''; 
  } 
  echo'
".$val."
".$val."".$val."
'; echo''; echo''; } } $map=array('No','Name','Email'); $datal=array(array(1, '管理员', 'admin@163.com'), array(2, 'member', 'member@163.com'));; $csv=new toExcel; $csv->toExcel($map,$datal,"dataexport"); ?>

方法二

<?php 
header("Content-Type: application/vnd.ms-execl"); 
header("Content-Disposition: attachment; filename=myExcel.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
/*first line*/ 
$data1= "中文测试"; 
$data1=mb_convert_encoding($data1,"GB2312","UTF-8"); 
echo $data1."\t"; 
echo "world"."\t"; 
echo "\t\n"; 
/*start of second line*/ 
echo "this is second line"."\t"; 
echo "Hi,pretty girl"."\t"; 
echo "\t\n"; 
?>

希望本文所述对大家的php程序设计有所帮助。

人气教程排行