<?php
header('Content-Type: application/vnd.ms-excel');
header("Accept-Ranges:bytes");
header('Content-Disposition: attachment; filename=demo.xls');
header('Pragma: no-cache');
header('Expires: 0');
$title = array(
'chief_id' => '工長ID',
'name' => '工長姓名',
'mobile' => '工長電話',
'Invitation' => '是否受邀',
'sign' => '簽到情況',
'sign_time' => '簽到時間',
'imei' => '設備信息'
);
$sign_in_list= Array
(
[0] => Array
(
[chief_id] => 5
[name] => 測試臨時表2
[mobile] => 13520037563
[Invitation] => 是
[sign] => √
[sign_time] => 1478576359
[imei] => 測試1
)
)
Array_unshift($sign_in_list, $title);
echo iconv('utf-8', 'gbk', implode("\t", $title)), "\n";
foreach ($sign_listas $value) {
echo iconv('utf-8', 'gbk', implode("\t", $value)), "\n";
}
echo '<head>
<!--[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>'; //不加head標簽 導出excel後沒有網格線
echo "<table>";
foreach ($sign_in_list as $key=>$value) {
echo "<tr>";
echo "<td>".iconv('utf-8', 'gbk', $value['chief_id'])."</td>";
echo "<td>".iconv('utf-8', 'gbk', $value['name'])."</td>"."\t";
echo "<td>".iconv('utf-8', 'gbk', $value['mobile'])."</td>"."\t";
echo "<td>".iconv('utf-8', 'gbk', $value['Invitation'])."</td>"."\t";
echo "<td>".iconv('utf-8', 'gbk', $value['sign'])."</td>"."\t";
echo "<td>".iconv('utf-8', 'gbk', $value['sign_time'])."</td>"."\t";
echo "<td>".iconv('utf-8', 'gbk', $value['imei'])."</td>"."\t";
echo "</tr>";
echo "\n";
//echo iconv('utf-8', 'gbk', implode("\t", $value)), "\n";
}
echo "</table>";
/*
1) 文本:vnd.ms-excel.numberformat:@
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
3) 數字:vnd.ms-excel.numberformat:#,##0.00
4) 貨幣:vnd.ms-excel.numberformat:¥#,##0.00
5) 百分比:vnd.ms-excel.numberformat: #0.00%
用法<td style='vnd.ms-excel.numberformat:@'>".$printable."</td> 主要解決數據格式問題
上述兩種都是最簡單的PHP導出excel方法 都會有瑕疵
第一種 如果像手機號一類的文本數據 會顯示科學計數法 如果用鼠標拉伸表格長度會完全顯示
兩種方法生成的excel文件再打開的時候都會提示
目前還沒解決 哪位大神有好的解決辦法可以交流
*/
?>