0

I have a problem and i was wondering if anyone can help me. Any help is really appreciated. And i m sure that its an 'easy' question.

I have a jsp page that shows dynamic data in tables, my problem is that the page has multiple tables. But i want to export to excel only a specific table with a specific id.

So far i m exporting to excel all data from the jsp page which is wrong, cause i want to export only one table with id="formAssignDemands", how can i do it ?

Here is my code,

 <!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 Answer 1

1

i managed to do it with javascript and is working on 3 basic browsers mozilla, chrome and IE11 , so here it is,

 <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> 

and to call it somewere in your body can add the following with an iframe, like that,

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

thats it , thank you guys for reading

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.