程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> php導出excel格式數據 實現教程

php導出excel格式數據 實現教程

編輯:PHP基礎知識
 

解決2個問題:

1.身份證之類的文本數據自動轉為科學計數法的問題。

2.中文亂碼的問題

excel從web頁面上導出的原理。當我們把這些數據發送到客戶端時,我們想讓客戶端程序(浏覽器)以excel的格式讀取 它,所以把mime類型設為:application/vnd.ms-excel,當excel讀取文件時會以每個cell的格式呈現數據,如果cell沒有規定的格式,則excel會以默認的格式去呈現該cell的數據。這樣就給我們提供了自定義數據格式的空間,當然我們必須使用excel支持的格式。 下面就列出常用的一些格式:
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%
這些格式你也可以自定義,比如年月你可以定義為:yy-mm等等。那麼知道了這些格式,怎麼去把這些格式添加到cell中呢?很簡單,我們只需要把樣式添 加到對應的標簽對(即閉合標簽)即可。如<td></td>,給標簽對<td></td>添加樣式,如 下: <td style="vnd.ms-excel.numberformat:@">410522198402161833</td>
同樣,我們也可以給<div></div>添加樣式,也可以給<tr>< /tr>,<table></table>添加樣式;當我們在父標簽對和子標簽對都添加樣式時,數據會以哪一個樣式呈現 呢?經過測試,會以離數據最近的樣式呈現.

例如身份證列的<td>的樣式:

echo "<td style='vnd.ms-excel.numberformat:@'>".$printable."</td>\n";

$filename=iconv("UTF-8", "GB2312//IGNORE","會員名.xls");//date('Y-m-d-H-i-s').".xls";
header("Content-type:application/vnd.ms-excel");
Header("Accept-Ranges:bytes");
Header("Content-Disposition:attachment;filename=".$filename); //$filename導出的文件名
header("Pragma: no-cache");
header("Expires: 0");

echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<!--[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>';
echo "<table><tr>
<th>".iconv("UTF-8", "GB2312//IGNORE","會員名")."</th>
<th>".iconv("UTF-8", "GB2312//IGNORE","賬號")."</th>
<th>".iconv("UTF-8", "GB2312//IGNORE","聯系人")."</th>
</tr>";
foreach ($list as $v)
{
echo "<tr>";
echo "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["user_name"])."</td>";
echo "<td style='vnd.ms-excel.numberformat:@'>".$v["account_id"]."</td>";
echo "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["contact_name"])."</td>";
echo "</tr>";
}
echo "</table>";

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved