導出word文檔
第一步:編輯好word模版,然后另存為*.htm,比如:liukun.htm。
技巧:在需要填寫數據的地方最好預填入一些易識別的數據,這樣方便后面填寫jsp代碼。
第二步:把htm后綴改為jsp,比如:liukun.jsp。
第三步:添加jsp的頭,比如:
<%@page contentType="application/msword;charset=GBK"%>
如果有import,也要在這里導入。
技巧:application/msword;這個參數很重要,有了這個參數,調用這個頁面時,就會把頁面內容存為word。當然,本地必須安裝office。
導出Excel文檔
只需要在jsp的最上面加上一句話




就可以將網頁的內容導出為Excel。
目前給出的例子為了方便起見,就是使用了純粹的靜態頁面,一個table其中有一行是標題,一行是內容,但是實際使用中不可能這么簡單,都是保持靜態的內容,如果需要保存的內容是從數據庫中取出,則只需要循環遍歷取出的內容,添加行就行了,假如從數據庫中取出的數據存入UserList中,可以使用struts標簽進行遍歷如下:
<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>
<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>
下面是完整的例子,新建一個index.jsp,里面只需要一個超鏈接<a href = 'DownLoadExcel.jsp'>導出Excel</a>
再新建一個DownLoadExcel.jsp,內容如下:
<%
response.reset();
response.setContentType("application/vnd.ms-excel;charset=GBK");
%>
<html>
<head>
<title>刷卡消費情況</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%">北京中關村</td>
</tr>
</table>
</form>
</body>
</html>
response.reset();
response.setContentType("application/vnd.ms-excel;charset=GBK");
%>
<html>
<head>
<title>刷卡消費情況</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%">北京中關村</td>
</tr>
</table>
</form>
</body>
</html>
部署好程序,在index.jsp中點擊超鏈接就可以完成導出了!