php導出excel長數字顯示成科學計數法格式的解決方案php
首先,咱們瞭解一下excel從web頁面上導出的原理。當咱們把這些數據發送到客戶端時,咱們想讓客戶端程序(瀏覽器)以excel的格式讀取它,因此把mime類型設爲:application/vnd.ms-excel,當excel讀取文件時會以每一個cell的格式呈現數據,若是cell沒有規定的格式,則excel會以默認的格式去呈現該cell的數據。這樣就給咱們提供了自定義數據格式的空間,固然咱們必須使用excel支持的格式。下面就列出經常使用的一些格式:html
1) 文本:vnd.ms-excel.numberformat:@mysql
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/ddweb
3) 數字:vnd.ms-excel.numberformat:#,##0.00sql
4) 貨幣:vnd.ms-excel.numberformat:¥#,##0.00瀏覽器
5) 百分比:vnd.ms-excel.numberformat: #0.00%app
這些格式你也能夠自定義,好比年月你能夠定義爲:yy-mm等等。那麼知道了這些格式,怎麼去把這些格式添加到cell中呢?很簡單,咱們只須要把樣式添加到對應的標籤對(即閉合標籤)便可。如<td></td>,給標籤對<td></td>添加樣式,以下:ide
<tdstyle="vnd.ms-excel.numberformat:@">410522198402161833</td>測試
一樣,咱們也能夠給<div></div>添加樣式,也能夠給<tr></tr>,<table></table>添加樣式,這樣就會引入一個問題,你注意到了嗎?先看以下的代碼:fetch
<tablestyle=’vnd.ms-excel.numberformat:#,##0.00’>
<tr>
<td>542</td>
<tdstyle=’vnd.ms-excel.numberformat: #0.00%’>0.25</td>
</tr>
</table>
當咱們在父標籤對和子標籤對都添加樣式時,數據會以哪個樣式呈現呢?通過測試,會以離數據最近的樣式呈現,這也是符合咱們的意願的(好像也符合一句俗話:縣官不如現管)。這樣咱們就能夠經過改變樣式而改變數據在excel中呈現的方式(這些樣式規格你能夠在前臺頁面上添加也能夠在後臺代碼裏給相應的控件如:DataGrid等添加這些樣式)。若是你的應用比較簡單,那麼這已經足夠知足你的需求。但若是你的應用比較複雜,那麼你也能夠採起一種方式來達到不一樣的數據呈現效果。下面,我就舉一個稍微複雜一點的應用。
例如:你的數據要呈現給不一樣國家和地區的用戶查看,這樣數據的呈現的格式就會不同,那麼咱們怎麼解決這個問題呢?固然了,你能夠手工把這些數據處理好,但這畢竟不是最好的方法,由於若是咱們每增長一個其餘國家或地區的用戶,那麼咱們就須要把全部的數據以客戶要求的格式處理一遍,當數據量很大時,這無疑是一件很沉重且無聊的工做。那麼咱們究竟應該怎樣解決相似這樣的問題呢?下面我說一下,我本身的見解:把這些格式化的信息抽取到一個xml文件中,程序運行時根據不一樣的客戶讀取不一樣的格式化信息,而後把這些格式化信息動態的添加到咱們的數據上,這樣,當咱們每增長一個其餘國家或地區的用戶時,咱們只須要多增長一個xml文件,把對應的格式化信息寫入這個xml文件,而後當這個國家或地區的用戶查看時,就把對應的格式化信息讀取出來應用到數據上便可。
php導出excel實例以下:
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<?php
header('Content-Type:application/vnd.ms-excel;charset=gbk');
header("Content-Disposition:p_w_upload;filename=coupon_all.xls");
$tx='<span style="font-weight:bold;color:#ff0000;font-size:15px;">列表</span>';
echo$tx."\n\n";
echo"<table border=1 width=1002 style='text-align:center'>";
echo"\n";
echo"<tr>";
echo"<td></td>"."\t";
echo"</tr>";
echo"\n";
echo"<tr style='background:#DDECE9;font-weight:bold;'>";
echo"<td>序號</td>"."\t";
echo"<td>帳號</td>"."\t";
echo"<td>真實姓名</td>"."\t";
echo"<td>單位</td>"."\t";
echo"<td>×××號</td>"."\t";
echo"<td>省份</td>"."\t";
echo"<td>城市</td>"."\t";
echo"<td>手機</td>"."\t";
echo"<td>QQ</td>"."\t";
echo"<td>申請時間</td>"."\t";
echo"<td>備註</td>"."\t";
echo"\n";
echo"</tr>";
include('../conn.php');
$sql="select * from `chit` order by `createtime` desc;";
$query=mysql_query($sql,$conn);
$i=1;
while($row=mysql_fetch_array($query)){
echo"\n";
if($row['is_verify']=='-1'){
echo"<tr style='background:#FF8080'>";
}
elseif($row['is_verify']=='1'){
echo"<tr style='background:#D0E8FF'>";
}
else{
echo"<tr>";
}
echo"<td>".$i++."</td>"."\t";
echo"<td>".$row['username']."</td>"."\t";
echo"<td>".$row['realname']."</td>"."\t";
echo"<td>".$row['danweiname']."</td>"."\t";
echo"<td style='vnd.ms-excel.numberformat:@'>".$row['idcard']."</td>"."\t";
echo"<td>".$row['province']."</td>"."\t";
echo"<td>".$row['city']."</td>"."\t";
echo"<td>".$row['phone']."</td>"."\t";
echo"<td>".$row['qq']."</td>"."\t";
echo"<td>".date("Y-m-d H:i:s",$row['createtime'])."</td>"."\t";
echo"<td>".$row['verify_content']."</td>"."\t";
echo"</tr>";
echo"\n";
}
echo"</table>";
?>