分类分类
更新时间:2026-04-09 15:24:39作者:zhao
本文实例讲述了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 "tn";
/*start of second line*/
echo "this is second line"."t";
echo "Hi,pretty girl"."t";
echo "tn";
?>
希望本文所述对大家的php程序设计有所帮助。
相关
傲视神魔传手游策略游戏13.55 Mv1.0.02026-04-09
下载三国志王道天下策略游戏2.18Gv0.9.8.12026-04-09
下载风云三国手机版策略游戏213.99 M2026-04-09
下载星之海手机版角色扮演2.78Gv1.1.598772026-04-09
下载迪士尼梦幻王国经营养成79.34 Mv11.5.0h2026-04-09
下载TapAim动作射击97.92 Mv2.0.12026-04-09
下载肖邦大冒险九游版策略游戏133.64 Mv1.02026-04-09
下载放松时光与你共享Lo-Fi故事休闲益智951.66 Mv1.4.62026-04-09
下载羽毛球对决体育竞技175.66 Mv4.12026-04-09
下载假面骑士泽兹变身模拟器休闲益智51.91 Mv1.00.302026-04-09
下载餐车厨师烹饪游戏苹果版模拟游戏327.2 Mv8.682026-04-09
下载最终幻想7永恒危机ios版角色游戏1.5Gv3.6.02026-04-09
下载










