php導(dǎo)出excel格式數(shù)據(jù)問題
解決2個問題:
1.身份證之類的文本數(shù)據(jù)自動轉(zhuǎn)為科學計數(shù)法的問題。
2.中文亂碼的問題
excel從web頁面上導(dǎo)出的原理。當我們把這些數(shù)據(jù)發(fā)送到客戶端時,我們想讓客戶端程序(瀏覽器)以excel的格式讀取 它,所以把mime類型設(shè)為:application/vnd.ms-excel,當excel讀取文件時會以每個cell的格式呈現(xiàn)數(shù)據(jù),如果cell沒有規(guī)定的格式,則excel會以默認的格式去呈現(xiàn)該cell的數(shù)據(jù)。這樣就給我們提供了自定義數(shù)據(jù)格式的空間,當然我們必須使用excel支持的格式。 下面就列出常用的一些格式:
1) 文本:vnd.ms-excel.numberformat:@
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
3) 數(shù)字:vnd.ms-excel.numberformat:#,##0.00
4) 貨幣:vnd.ms-excel.numberformat:¥#,##0.00
5) 百分比:vnd.ms-excel.numberformat: #0.00%
這些格式你也可以自定義,比如年月你可以定義為:yy-mm等等。那么知道了這些格式,怎么去把這些格式添加到cell中呢?很簡單,我們只需要把樣式添 加到對應(yīng)的標簽對(即閉合標簽)即可。如<td></td>,給標簽對<td></td>添加樣式,如 下: <td style="vnd.ms-excel.numberformat:@">410522198402161833</td>
同樣,我們也可以給<div></div>添加樣式,也可以給<tr>< /tr>,<table></table>添加樣式;當我們在父標簽對和子標簽對都添加樣式時,數(shù)據(jù)會以哪一個樣式呈現(xiàn) 呢?經(jīng)過測試,會以離數(shù)據(jù)最近的樣式呈現(xiàn).
例如身份證列的<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導(dǎo)出的文件名
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","聯(lián)系人")."</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>";
- 利用phpExcel實現(xiàn)Excel數(shù)據(jù)的導(dǎo)入導(dǎo)出(全步驟詳細解析)
- php導(dǎo)入導(dǎo)出excel實例
- php中導(dǎo)出數(shù)據(jù)到excel時數(shù)字變?yōu)榭茖W計數(shù)的解決方法
- php將數(shù)據(jù)庫導(dǎo)出成excel的方法
- 使用PHPExcel實現(xiàn)數(shù)據(jù)批量導(dǎo)出為excel表格的方法(必看)
- php把數(shù)據(jù)表導(dǎo)出為Excel表的最簡單、最快的方法(不用插件)
- PHP實現(xiàn)導(dǎo)出帶樣式的Excel
- php導(dǎo)出word文檔與excel電子表格的簡單示例代碼
- php原生導(dǎo)出excel文件的兩種方法(推薦)
- PHP將Excel導(dǎo)入數(shù)據(jù)庫及數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出至Excel的方法
- php中通用的excel導(dǎo)出方法實例
- php 自定義函數(shù)實現(xiàn)將數(shù)據(jù) 以excel 表格形式導(dǎo)出示例
相關(guān)文章
PHP實現(xiàn)處理三級分類數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細介紹了如何利用PHP實現(xiàn)處理三級分類數(shù)據(jù),文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下2024-02-02php curl 登錄163郵箱并抓取郵箱好友列表的代碼(經(jīng)測試)
PHP模擬登陸獲取163郵箱聯(lián)系人的實現(xiàn)代碼,需要的朋友可以參考下。2011-04-04PHP中str_replace函數(shù)使用小結(jié)
在實際的程序開發(fā)中,執(zhí)行字符串替換操作是一件非常經(jīng)常的事,對str_replace函數(shù)的實用也會非常頻繁。2008-10-10