将jsp特定表id导出为ex​​cel

时间:2015-04-07 09:39:51

标签: excel jsp

我有一个问题,我想知道是否有人可以帮助我。任何帮助都非常感谢。我确信这是一个“简单”的问题。

我有一个显示表中动态数据的jsp页面,我的问题是页面有多个表。 但我想只导出excel一个具有特定id的特定表。

到目前为止,我导出了来自jsp页面的所有数据,这是错误的,因为我只想导出一个id =“formAssignDemands”的表,我该怎么办?

这是我的代码,

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">      
     <%@ page contentType="text/html;charset=windows-1253"%>
     <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
     <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
     <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
     <%@ taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>
      <f:view>
         <html>
           <head>
             <meta http-equiv="Content-Type" content="text/html; charset=windows-1253"/>
          <f:loadBundle basename="bundles.requests" var="FieldsDescr"/>
          <f:loadBundle basename="bundles.messages" var="GeneralMessages"/>
          <title>
            <h:outputText value="#{FieldsDescr.AssignRequests_Title}"/>
          </title>
          <a4j:loadStyle src="#{AssignDemands_backing.municipality.url}#{AssignDemands_backing.municipality.templateId}#{GeneralMessages.CssPath}"/>
          <script language="JavaScript" type="text/javascript"
                src="./demands.js"></script>
          <script language="JavaScript" type="text/javascript"
                src="../reports/reports.js"></script>
          </head>
           <body><h:form>
            <jsp:include page="/system/NavigationMenu.jsp"/>
            </h:form>

            <%
            String exportToExcel = request.getParameter("exportToExcel");
            if (exportToExcel != null && exportToExcel.toString().equalsIgnoreCase("YES")) {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "inline; filename="
                + "excel.xls");

           }
         %>

            <table cellspacing="0" cellpadding="0" border="0"
                        width="99%" align="center">
              <tr>
                  <td class="componentheading" width="99%">
                      <h:outputText value="#{FieldsDescr.AssignRequests_SubHeader}"/>
                 </td>
                <td class="componentheading" width="1%">
                    <table>

             ......

               <tr>
                <td class="main_component_seperator" colspan="2">&nbsp;</td>
               </tr>
               <tr>
                <td align="center" colspan="2">
                    <h:form id="formAssignDemands">
                        <t:dataTable id="demandsDataTable"
                                     binding="#{AssignDemands_backing.demandsDataTable}"
                                     var="demand"
                                     value="#{AssignDemands_backing.unassignedDemandsList}"
                                     rows="#{NavigationMenu_backing.rows}"
                                     varDetailToggler="detailToggler"
                                     sortColumn="#{AssignDemands_backing.sort}"
                                     sortAscending="#{AssignDemands_backing.ascending}"
                                     preserveSort="true"
                                     headerClass="datatable_td_header"
                                     rowClasses="tablerow1, tablerow2"
                                     width="100%"
                                     rowIndexVar="demandsRowIndex">
                            <f:facet name="header">
                                <h:panelGroup>
                                    <f:verbatim>
                                        <div style="float:left;"></div>
                                    </f:verbatim>
                                    <h:panelGrid columns="4"
                                                 cellspacing="2">
                                        <h:outputText value="#{GeneralMessages.DataTable_RowsPerPage}"/>

                   .....

             </table>

        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <%
    if (exportToExcel == null) {
    %>
    <a href="AssignDemands.jsp?exportToExcel=YES">Export to Excel</a>
    <%
     }
    %>

     </body>
   </html>
   </f:view>
  <%-- oracle-jdev-comment:auto-binding-backing-bean-name:AssignRequests--%>

     Thank you guys for any help or any idea,,

1 个答案:

答案 0 :(得分:1)

我设法用javascript完成它并且正在开发3个基本浏览器mozilla,chrome和IE11,所以在这里,

         <script type="text/javascript">
         function ExcelReport()
         {
         var tab_text="<table border='2px'><tr bgcolor='#FFFFFF'>";
         var textRange; var j=0;
         tab = document.getElementById('table_id'); // id of table

for(j = 0 ; j < tab.rows.length ; j++) 
{     
    tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";       
}

tab_text=tab_text+"</table>";
tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");
tab_text= tab_text.replace(/<img[^>]*>/gi,""); 
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); 
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE "); 

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      
{
    txtArea1.document.open("txt/html","replace");
    txtArea1.document.write(tab_text);
    txtArea1.document.close();
    txtArea1.focus(); 
    sa=txtArea1.document.execCommand("SaveAs",true,"Excel.xls");
}  
else                 
    sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

   return (sa);
  }

  </script>         

并在你的身体中调用它可以添加以下iframe,就像那样,

    <iframe id="txtArea1" style="display:none"></iframe>    
    <button id="btnExport" class="button_class" onclick="ExcelReport();">Export to Excel</button>

就是这样,谢谢你们阅读