• JSP導出Excel報表的簡單方法

    2
    Java HTML C/C++ JSP ci 18528 次瀏覽

    在Web應用中,很多數據經常要導出成Excel文檔。用專門的生成真正的Excel文檔的方式比較復雜,不太好用。所以經常用一種簡單的方式來實 現,即將報表保存為HTML格式,然后用Excel打開。 實現方式:    第一步,用JSP實現HTML版本的報表    第二步,在該JSP頁面頭部設置response的ContentType為Excel格式           
    <% response.setContentType("application/vnd.ms-excel;charset=GBK"); %>  
    中文問題:    查看源代碼時發現JSP文件中寫死的中文為亂碼,則在JSP文件頭部添加一行       
    <%@ page contentType="text/html; charset=gb2312" %>   
    查看源代碼時發現文字為中文,但是用Excel打開為亂碼則在<html>與<head>中加入       
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">       
    用Servlet實現也是類似的處理方法。 

    實現樣例:

    <%@ page contentType="text/html; charset=UTF-8" %>
    <% response.setContentType("application/vnd.ms-excel;charset=UTF-8"); %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <body>
     <table cellpadding="1" cellspacing="1"  border="1">
      <tr>
      <td colspan="5" align="center">
      ${ticketname}
      </td>
      </tr>
      <tr class="dan_tr">
       <th>使用時間</th>
       <th>使用者</th>
       <th>傳播者</th>
       <th>使用地點</th>
       <th>消耗積分</th>
      </tr>
      <c:forEach var="list" items="${list}">
      <tr align="center">
       <td width="135">${list.userDate}</td>
       <td width="100">${list.userName}</td>
       <td width="100">${list.puserName}</td>
       <td width="350">${list.userCorp}</td>
       <td>${list.integral}分</td>
      </tr>
      </c:forEach>
     </table>
    </body>

    相似問題

    相關經驗

    相關資訊

    相關文檔

  • sesese色