隨筆 - 147  文章 - 71  trackbacks - 0
          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類(146)

          隨筆檔案(147)

          文章分類(28)

          文章檔案(28)

          喜歡的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          導出word文檔

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

          導出Excel文檔

          只需要在jsp的最上面加上一句話

          <%
             response.reset(); 
             response.setContentType(
          "application/vnd.ms-excel;charset=GBK");
          %>

          就可以將網頁的內容導出為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>

           下面是完整的例子,新建一個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>

           

          部署好程序,在index.jsp中點擊超鏈接就可以完成導出了!
          posted on 2011-12-23 09:27 飛翔天使 閱讀(459) 評論(0)  編輯  收藏 所屬分類: JSP
          主站蜘蛛池模板: 黔南| 通州市| 体育| 澳门| 额济纳旗| 绥化市| 西盟| 苗栗市| 清原| 广汉市| 鹤山市| 汝州市| 平远县| 于都县| 泉州市| 宁陕县| 衡阳县| 博客| 舟山市| 定边县| 隆尧县| 舞阳县| 府谷县| 遵化市| 安图县| 桑植县| 涪陵区| 额济纳旗| 浦江县| 常德市| 刚察县| 涿鹿县| 汽车| 桦南县| 宝坻区| 苏州市| 将乐县| 黄骅市| 永寿县| 柘城县| 阿拉善盟|