PHP導出EXCEL
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='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('120760130129512044', '管理員', '[email protected]'), array('120760130129512055', 'member', '[email protected]'));;
$csv=new toExcel;
$csv->toExcel($map,$datal,"dataexport");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.