jsp匯出excel檔案並設定儲存格格式
來源:互聯網
上載者:User
<%@ page contentType="application/msexcel" %>
<!-- 以上這行設定本網頁為excel格式的網頁 -->
<%
response.setHeader("Content-disposition","inline; filename=test1.xls");
//以上這行設定傳送到前端瀏覽器時的檔名為test1.xls
//就是靠這一行,讓前端瀏覽器以為接收到一個excel檔
%>
<html>
<head>
<title>Excel檔案呈現方式</title>
<style>
.xlsText{mso-number-format:"\@";}
</style>
</head>
<body>
<table border="1" width="100%">
<tr>
<td>姓名</td><td>身份證字型大小</td><td>生日</td>
</tr>
<tr>
<td>李玟</td><td class="xlsText">00111111111</td><td>1900/11/12</td>
</tr>
<tr>
<td>梁靜如</td><td>00222222222</td><td>1923/10/1</td>
</tr>
<tr>
<td>張惠妹</td><td>00333333333</td><td>1934/12/18</td>
</tr>
</table>
</body>
</html>
如果沒有設定儲存格顯示格式的話,數字預設是不顯示前面的0的,如“001”會顯示為1,可以設定儲存格顯示格式為文體,就可以顯示出前面的0了
其他格式設定如下:
Styling Excel cells with mso-number-format
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)