導(dǎo)出word文檔
第一步:編輯好word模版,然后另存為*.htm,比如:liukun.htm。
技巧:在需要填寫數(shù)據(jù)的地方最好預(yù)填入一些易識(shí)別的數(shù)據(jù),這樣方便后面填寫jsp代碼。
第二步:把htm后綴改為jsp,比如:liukun.jsp。
第三步:添加jsp的頭,比如:
<%@page contentType="application/msword;charset=GBK"%>
如果有import,也要在這里導(dǎo)入。
技巧:application/msword;這個(gè)參數(shù)很重要,有了這個(gè)參數(shù),調(diào)用這個(gè)頁(yè)面時(shí),就會(huì)把頁(yè)面內(nèi)容存為word。當(dāng)然,本地必須安裝office。
導(dǎo)出Excel文檔
只需要在jsp的最上面加上一句話
<%
response.reset();
response.setContentType("application/vnd.ms-excel;charset=GBK");
%>
就可以將網(wǎng)頁(yè)的內(nèi)容導(dǎo)出為Excel。
目前給出的例子為了方便起見,就是使用了純粹的靜態(tài)頁(yè)面,一個(gè)table其中有一行是標(biāo)題,一行是內(nèi)容,但是實(shí)際使用中不可能這么簡(jiǎn)單,都是保持靜態(tài)的內(nèi)容,如果需要保存的內(nèi)容是從數(shù)據(jù)庫(kù)中取出,則只需要循環(huán)遍歷取出的內(nèi)容,添加行就行了,假如從數(shù)據(jù)庫(kù)中取出的數(shù)據(jù)存入U(xiǎn)serList中,可以使用struts標(biāo)簽進(jìn)行遍歷如下:
<table class="common1" cellpadding="5" cellspacing="1" align="center" >
<tr>
<td class=formtitle colspan="4"><CENTER> 清單</CENTER> </td>
</tr>
<tr>
<td class=formtitle align="center" nowrap style="width:13%">姓名</td>
<td class=formtitle align="center" nowrap style="width:13%">年齡</td>
<td class=formtitle align="center" nowrap style="width:13%">性別</td>
<td class=formtitle align="center" nowrap style="width:13%">住址</td>
</tr>
<logic:present name="UserList">
<logic:iterate id="user" name="UserList">
<tr>
<td align="center" nowrap style="width:13%">
<bean:write name = "user",property="name"/>
</td>
<td align="center" nowrap style="width:13%">
<bean:write name = "user",property="age"/>
</td>
<td align="center" nowrap style="width:13%">
<bean:write name = "user",property="sex"/>
</td>
<td align="center" nowrap style="width:13%">
<bean:write name = "user",property="address"/>
</td>
</tr>
</logic:iterate>
</logic:present>
</table>
下面是完整的例子,新建一個(gè)index.jsp,里面只需要一個(gè)超鏈接<a href = 'DownLoadExcel.jsp'>導(dǎo)出Excel</a>
再新建一個(gè)DownLoadExcel.jsp,內(nèi)容如下:
<%
response.reset();
response.setContentType("application/vnd.ms-excel;charset=GBK");
%>
<html>
<head>
<title>刷卡消費(fèi)情況</title>
<style type="text/css">
table.common1 {
width: 100%;
font-size: 9pt;
style-align: center;
background-color: #ffffff;
}
td.formtitle {
font-size: 9pt;
background:#a480b2;
color:#ffffff;
height:30px;
text-align: center;
}
</style>
</head>
<body>
<form name="fm" method="post" >
<table class="common1" cellpadding="5" cellspacing="1" align="center" >
<tr>
<td class=formtitle colspan="4"><CENTER> 清單</CENTER> </td>
</tr>
<tr>
<td class=formtitle align="center" nowrap style="width:13%">姓名</td>
<td class=formtitle align="center" nowrap style="width:13%">年齡</td>
<td class=formtitle align="center" nowrap style="width:13%">性別</td>
<td class=formtitle align="center" nowrap style="width:13%">家庭住址</td>
</tr>
<tr>
<td align="center" nowrap style="width:13%">張三</td>
<td align="center" nowrap style="width:13%">25</td>
<td align="center" nowrap style="width:13%">男</td>
<td align="center" nowrap style="width:13%">北京中關(guān)村</td>
</tr>
</table>
</form>
</body>
</html>
部署好程序,在index.jsp中點(diǎn)擊超鏈接就可以完成導(dǎo)出了!
posted on 2011-12-23 09:27
飛翔天使 閱讀(458)
評(píng)論(0) 編輯 收藏 所屬分類:
JSP