php導出excel長數字顯示成科學計數法格式的解決方案

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

  
  
           
  
  
  1. <tdstyle="vnd.ms-excel.numberformat:@">410522198402161833</td>測試


一樣,咱們也能夠給<div></div>添加樣式,也能夠給<tr></tr>,<table></table>添加樣式,這樣就會引入一個問題,你注意到了嗎?先看以下的代碼:fetch


  
  
           
  
  
  1. <tablestyle=’vnd.ms-excel.numberformat:#,##0.00’>

  2. <tr>

  3. <td>542</td>

  4. <tdstyle=’vnd.ms-excel.numberformat: #0.00%’>0.25</td>

  5. </tr>

  6. </table>


當咱們在父標籤對和子標籤對都添加樣式時,數據會以哪個樣式呈現呢?通過測試,會以離數據最近的樣式呈現,這也是符合咱們的意願的(好像也符合一句俗話:縣官不如現管)。這樣咱們就能夠經過改變樣式而改變數據在excel中呈現的方式(這些樣式規格你能夠在前臺頁面上添加也能夠在後臺代碼裏給相應的控件如:DataGrid等添加這些樣式)。若是你的應用比較簡單,那麼這已經足夠知足你的需求。但若是你的應用比較複雜,那麼你也能夠採起一種方式來達到不一樣的數據呈現效果。下面,我就舉一個稍微複雜一點的應用。

      例如:你的數據要呈現給不一樣國家和地區的用戶查看,這樣數據的呈現的格式就會不同,那麼咱們怎麼解決這個問題呢?固然了,你能夠手工把這些數據處理好,但這畢竟不是最好的方法,由於若是咱們每增長一個其餘國家或地區的用戶,那麼咱們就須要把全部的數據以客戶要求的格式處理一遍,當數據量很大時,這無疑是一件很沉重且無聊的工做。那麼咱們究竟應該怎樣解決相似這樣的問題呢?下面我說一下,我本身的見解:把這些格式化的信息抽取到一個xml文件中,程序運行時根據不一樣的客戶讀取不一樣的格式化信息,而後把這些格式化信息動態的添加到咱們的數據上,這樣,當咱們每增長一個其餘國家或地區的用戶時,咱們只須要多增長一個xml文件,把對應的格式化信息寫入這個xml文件,而後當這個國家或地區的用戶查看時,就把對應的格式化信息讀取出來應用到數據上便可。

php導出excel實例以下:

  
  
           
  
  
  1. <meta http-equiv="Content-Type" content="text/html; charset=GBK" />

  2. <?php

  3. header('Content-Type:application/vnd.ms-excel;charset=gbk');    

  4. header("Content-Disposition:p_w_upload;filename=coupon_all.xls");

  5. $tx='<span style="font-weight:bold;color:#ff0000;font-size:15px;">列表</span>';

  6. echo$tx."\n\n";  

  7. echo"<table border=1 width=1002 style='text-align:center'>";

  8. echo"\n";

  9. echo"<tr>";

  10. echo"<td></td>"."\t";

  11. echo"</tr>";

  12. echo"\n";

  13. echo"<tr style='background:#DDECE9;font-weight:bold;'>";

  14. echo"<td>序號</td>"."\t";

  15. echo"<td>帳號</td>"."\t";

  16. echo"<td>真實姓名</td>"."\t";  

  17. echo"<td>單位</td>"."\t";

  18. echo"<td>×××號</td>"."\t";

  19. echo"<td>省份</td>"."\t";

  20. echo"<td>城市</td>"."\t";

  21. echo"<td>手機</td>"."\t";

  22. echo"<td>QQ</td>"."\t";

  23. echo"<td>申請時間</td>"."\t";

  24. echo"<td>備註</td>"."\t";

  25. echo"\n";

  26. echo"</tr>";

  27. include('../conn.php');  

  28. $sql="select * from `chit` order by `createtime` desc;";

  29. $query=mysql_query($sql,$conn);

  30. $i=1;

  31. while($row=mysql_fetch_array($query)){

  32. echo"\n";

  33. if($row['is_verify']=='-1'){

  34. echo"<tr style='background:#FF8080'>";

  35.    }

  36. elseif($row['is_verify']=='1'){

  37. echo"<tr style='background:#D0E8FF'>";

  38.    }

  39. else{

  40. echo"<tr>";

  41.    }

  42. echo"<td>".$i++."</td>"."\t";

  43. echo"<td>".$row['username']."</td>"."\t";

  44. echo"<td>".$row['realname']."</td>"."\t";

  45. echo"<td>".$row['danweiname']."</td>"."\t";

  46. echo"<td style='vnd.ms-excel.numberformat:@'>".$row['idcard']."</td>"."\t";

  47. echo"<td>".$row['province']."</td>"."\t";

  48. echo"<td>".$row['city']."</td>"."\t";

  49. echo"<td>".$row['phone']."</td>"."\t";

  50. echo"<td>".$row['qq']."</td>"."\t";

  51. echo"<td>".date("Y-m-d H:i:s",$row['createtime'])."</td>"."\t";

  52. echo"<td>".$row['verify_content']."</td>"."\t";

  53. echo"</tr>";

  54. echo"\n";

  55. }

  56. echo"</table>";

  57. ?>

相關文章
相關標籤/搜索