網頁資料轉為Excel檔案


信件已寄出,盡快回覆您!

公開


網頁資料轉為Excel檔案

簡介:網頁資料輸出轉為Excel檔案

標籤:轉為Excel 程式設計 asp 程式模組 網路資料 程式工具
時間:2014/5/26 下午 01:31:58
查閱:3942 次

[CSS]網頁資料輸出轉為Excel檔案時數字資料強制為文字呈現(含其他mso支援格式轉換)

在網頁應用程式開發上常常會有需求是要把表格呈現的資料轉為excel儲存,而遇到這些需求的時候往往就是透過直接輸出一個表格的方式(html table tag)把資料透過tr,th,td的方式印出來。

而有時候在印出的資料為一個數字的時候會發生印出少0的情況,如035781178印出來會變成35781178,而資料如果是035-781178卻可以成功印出。其實這樣是因為excel column預設的general format會將數字開頭的0去除,而文字格式則不會有這個問題。

實務上,下面是一段可以將電話前端0印出的classic asp程式


<%
'設定輸出為excel格式
response.ContentType = "application/vnd.ms-excel"
'設定檔名
response.AddHeader "content-disposition", "attachment;  filename=Export.xls"
%>
<%
Set RS = conn.execute(session("CardApply_Sql"))
%>
<HTML>
'設定編碼,避免中文亂碼
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body>
<% 
Response.Write("<style type=text/css>")
Response.Write("td{mso-number-format:\@;}") '將所有td欄位格式改 為"文字"
Response.Write("</style>")
%>
<table border=1 style="font-size:12pt;">
<tr>
<th bgcolor="#d0d0d0">卡別</th>
<th bgcolor="#d0d0d0">卡號</th>
<th bgcolor="#d0d0d0">會員編號</th>
<th bgcolor="#d0d0d0">身分證字號</th>
<th bgcolor="#d0d0d0">姓名</th>
<th bgcolor="#d0d0d0">印製狀態</th>
<th bgcolor="#d0d0d0">E-mail</th> 
<th bgcolor="#d0d0d0">申請時間</th>  
<th bgcolor="#d0d0d0">聯絡地址</th> 
<th bgcolor="#d0d0d0">聯絡電話</th>
<th bgcolor="#d0d0d0">手機電話</th>
</tr>
<%
while not RS.EOF
    response.write "<tr>"
        response.write "<td align='left'>" & RS("CardType") & "</td>"
        response.write "<td align='left'>" & RS("CardNo") & "</td>"
        response.write "<td align='left'>" & RS("member_gicuitem") & "</td>"
        response.write "<td align='left'>" & RS("personalid") & "</td>"
        response.write "<td align='left'>" & RS("realname") & "</td>"
        response.write "<td align='left'>" & RS("email") & "</td>"
        response.write "<td align='left'>" & RS("xreffctupublic") & "</td>"         
        response.write "<td align='left'>" & RS("deditDate") & "</td>"
        response.write "<td align='left'>" & RS("address")&"</td>"
        response.write "<td align='left'>" & RS("telSection")&RS("tel") & "</td>"
        response.write "<td align='left'>" & RS("mobile") & "</td>"
    response.write "</tr>"
    RS.movenext
wend
%>
</table>
</body>
</html>


從上面可以看到,只要一段CSS就可以解決你的問題了,而如果你是要其他格式也可以幫你轉換,這邊幫大家找到一份整理資料如下:

mso-number-format:"0" NO Decimals
mso-number-format:"0\.000" 3 Decimals
mso-number-format:"\#\,\#\#0\.000" Comma with 3 dec
mso-number-format:"mm\/dd\/yy" Date7
mso-number-format:"mmmm\ d\,\ yyyy" Date9
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM
mso-number-format:"Short Date" 01/03/1998
mso-number-format:"Medium Date" 01-mar-98
mso-number-format:"d\-mmm\-yyyy" 01-mar-1998
mso-number-format:"Short Time" 5:16
mso-number-format:"Medium Time" 5:16 am
mso-number-format:"Long Time" 5:16:21:00
mso-number-format:"Percent" Percent - two decimals
mso-number-format:"0%" Percent - no decimals
mso-number-format:"0\.E+00" Scientific Notation
mso-number-format:"\@" Text
mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943)
mso-number-format:"\0022£\0022\#\,\#\#0\.00" £12.76
mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ "

2 decimals, negative numbers in red and signed
(1.56 -1.56)


----------轉為Excel 其他相關新聞