[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]HTML方式导出Excel文件,数字变科学计数法,解决方法

上一篇:[备忘]关于充值卡的卡号和密码的生成。
下一篇:[备忘]关于mysqldump导出的sql里的注释

添加日期:2014/9/21 20:35:47 快速返回   返回列表 阅读2800次
简单的说,就是使用mso-number-format来指定格式。
----------------------------------------------
在網頁應用程式開發上常常會有需求是要把表格呈現的資料轉為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)

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved