php匯出excel格式資料

來源:互聯網
上載者:User

解決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>";

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.