如何将JSP表导出为EXCEL(XLSX格式)

时间:2013-02-20 10:47:45

标签: html jsp format export-to-excel suppress-warnings

 <%@ page language="java" contentType="application/vnd.ms-excel"%>
   <% response.setHeader("Content-Disposition:","attachment;filename=Sample.xls"); %> 
 <TABLE>
  <TR><TD>2</TD></TR>
    <TR><TD>3</TD></TR>
   <TR><TD>5</TD></TR>
    </TABLE>

如何以XLSX格式导出JSP表以及如何抑制以下异常
您尝试打开的文件name.ext的格式与文件扩展名指定的格式不同。在打开文件之前,请验证文件是否已损坏且是否来自受信任的源。你想现在打开文件吗?

2 个答案:

答案 0 :(得分:1)

使用此

html代码

    <!DOCTYPE html>
    <html>
    <head>
      <title>tableToExcel Demo</title>
      <script>
 var tableToExcel = (function() {
      var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
        , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
      return function(table, name) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
        window.location.href = uri + base64(format(template, ctx))
      }
    })()
</script>
    </head>
    <body>

    <h1>tableToExcel Demo</h1>
    <p>Exporting the W3C Example Table</p>

    <input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel">

    <table id="testTable" summary="Code page support in different versions of MS Windows." rules="groups" frame="hsides" border="2"><caption>CODE-PAGE SUPPORT IN MICROSOFT WINDOWS</caption><colgroup align="center"></colgroup><colgroup align="left"></colgroup><colgroup span="2" align="center"></colgroup><colgroup span="3" align="center"></colgroup><thead valign="top"><tr><th>Code-Page<br>ID</th><th>Name</th><th>ACP</th><th>OEMCP</th><th>Windows<br>NT 3.1</th><th>Windows<br>NT 3.51</th><th>Windows<br>95</th></tr></thead><tbody><tr><td>1200</td><td style="background-color: #00f; color: #fff">Unicode (BMP of ISO/IEC-10646)</td><td></td><td></td><td>X</td><td>X</td><td>*</td></tr><tr><td>1250</td><td style="font-weight: bold">Windows 3.1 Eastern European</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1251</td><td>Windows 3.1 Cyrillic</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1252</td><td>Windows 3.1 US (ANSI)</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1253</td><td>Windows 3.1 Greek</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1254</td><td>Windows 3.1 Turkish</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1255</td><td>Hebrew</td><td>X</td><td></td><td></td><td></td><td>X</td></tr><tr><td>1256</td><td>Arabic</td><td>X</td><td></td><td></td><td></td><td>X</td></tr><tr><td>1257</td><td>Baltic</td><td>X</td><td></td><td></td><td></td><td>X</td></tr><tr><td>1361</td><td>Korean (Johab)</td><td>X</td><td></td><td></td><td>**</td><td>X</td></tr></tbody><tbody><tr><td>437</td><td>MS-DOS United States</td><td></td><td>X</td><td>X</td><td>X</td><td>X</td></tr><tr><td>708</td><td>Arabic (ASMO 708)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr><tr><td>709</td><td>Arabic (ASMO 449+, BCON V4)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr><tr><td>710</td><td>Arabic (Transparent Arabic)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr><tr><td>720</td><td>Arabic (Transparent ASMO)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr></tbody></table>

    </body>
    </html>

答案 1 :(得分:1)

使用这种方式:

<%@ page import="java.sql.* "  %>       
<%
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=SimDetails.xls");

Connection con = null;
PreparedStatement ps=null;
ResultSet  rs   = null;
%>
<%@ include file="directconnection.jsp"%>

<%
int simid=0;
int net=0;
String sim="";
String mob="";
String contnum="";
String couridate="";
String sendaddr="";
String networkName="";
String contpe="";

try{
    %>      
    <table>
        <tr>
            <th width="5%">SNo</th>
            <th width="30%;">Network</th>
            <th width="30%;">Sim Number</th>
            <th width="30%;">Mobile Number</th>
            <th width="30%;">Sender Address</th>
            <th width="30%;">Contact Person Number</th>
            <th width="30%;">Contact Person Name</th>
       </tr>
       <%
        String strSimExportQuery=" SELECT SM_SIM_ID,SM_PROVIDER_ID,SM_SIM_NUMBER,SM_MOBILE_NUMBER,"+
                                               " SM_SENDER_ADDRESS,SM_CONTACT_PERSON_NO,SM_CONTACT_PERSON FROM RS_SIM_MASTER "; 
        ps=con.prepareStatement(strSimExportQuery);
        rs=ps.executeQuery();
        while(rs.next()){
            simid++;
            net=rs.getInt("SM_PROVIDER_ID");
            if(net==1){
                networkName="Vodafone";
            }

            sim=rs.getString("SM_SIM_NUMBER");
            mob=rs.getString("SM_MOBILE_NUMBER");
            sendaddr=rs.getString("SM_SENDER_ADDRESS");
            contnum=rs.getString("SM_CONTACT_PERSON_NO");
            contpe=rs.getString("SM_CONTACT_PERSON");
            %>
            <tr>
                <td><%=simid%></td>
                <td><%=networkName%></td>
                <td><%=sim%></td>
                <td><%=mob%></td>
                <td><%=sendaddr%></td>
                <td><%=contnum%></td>
                <td><%=contpe%></td>
            </tr>
            <%}%>
         </table>
           <%
            }catch(Exception e){
                    out.println(e);
               }
                finally {
                    if (rs!= null){ rs.close(); rs=null;}
                    if(ps!=null){ps.close();ps=null;}
                    if (con != null) {con.close();con = null;}
            }
           %>