欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-20 10:38:13
本文实例讲述了php导出中文内容excel文件类。分享给大家供大家参考。具体如下:

本文实例讲述了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'<html xmlns:o="urn:schemas-microsoft-com:office:office" 
  xmlns:x="urn:schemas-microsoft-com:office:excel" 
  xmlns="[url=http://www.w3.org/tr/rec-html40]http://www.w3.org/tr/rec-html40[/url]"> 
  <head> 
  <meta http-equiv="expires" content="mon, 06 jan 1999 00:00:01 gmt"> 
  <meta http-equiv=content-type content="text/html; charset=utf-8"> 
  <!--[if gte mso 9]><xml> 
  <x:excelworkbook> 
  <x:excelworksheets> 
  <x:excelworksheet> 
  <x:name></x:name> 
  <x:worksheetoptions> 
  <x:displaygridlines/> 
  </x:worksheetoptions> 
  </x:excelworksheet> 
  </x:excelworksheets> 
  </x:excelworkbook> 
  </xml><![endif]--> 
  </head> 
  <body link=blue vlink=purple leftmargin=0 topmargin=0>'; 
  echo'<table border="0" cellspacing="0" cellpadding="0">'; 
  echo'<tr>'; 
  if(is_array($mapping)) { 
   foreach($mapping as $key=>$val) 
   echo"<td style='background-color:#09f;font-weight:bold;'>".$val."</td>"; 
  } 
  echo'</tr>'; 
  foreach($datalist as $k=>$v){ 
   echo'<tr>'; 
   foreach($v as $key=>$val){ 
    if(is_numeric($val) && strlen($val)>=14){ 
     echo"<td style='vnd.ms-excel.numberformat:@'>".$val."</td>"; //大于14位的数字转换成字符串输出(如身份证) 
    }else{ 
     echo"<td>".$val."</td>"; 
    } 
   } 
   echo'</tr>'; 
  } 
  echo'</table>'; 
  echo'</body>'; 
  echo'</html>'; 
 } 
} 
$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程序设计有所帮助。