Posts Tagged ‘excel

20
Dec
09

Generating an Excel Sheet using JSF

This page describes the process of generating an excel sheet using JSF dataTable custom tag.

Requirements

  • Maven
  • Basic Understanding of JSF

Installation

The following should be inserted into the pom.xml file.

	<dependency>
		<groupId>org.apache.myfaces.core</groupId>
		<artifactId>myfaces-api</artifactId>
		<version>1.2.8</version>
	</dependency>

	<dependency>
		<groupId>org.apache.myfaces.tomahawk</groupId>
		<artifactId>tomahawk</artifactId>
		<version>1.1.9</version>
	</dependency>

	<dependency>
		<groupId>org.apache.myfaces.core</groupId>
		<artifactId>myfaces-impl</artifactId>
		<version>1.2.8</version>
	</dependency>
	<dependency>
		<groupId>org.apache.tiles</groupId>
		<artifactId>tiles-jsp</artifactId>
		<version>2.1.4</version>
	</dependency>

The following is the bean used to generate the excel data

Coding the JSP

<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"  %>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t" %>

<f:subview id="genre_tile">

    <h3>Genre Table</h3>

    <h:form>
    <p>
    	<h:commandLink action="#{export.exportHtmlTableToExcel}">
		     <h:outputText value="Export table to Excel"/>
		</h:commandLink>    
    <t:buffer into="#{export.htmlBuffer}">
    	<t:dataTable styleClass="data_table" value="#{genreBean.allGenre}" var="genre">
    		<t:column sortable="true">
    		    <f:facet name="header">
		        	<h:outputText value="id" />
        		</f:facet> 
    			<h:outputText value="#{genre.id}"/>
    		</t:column>
    		<t:column sortable="true">
    		    <f:facet name="header">
		        	<h:outputText value="Name" />
        		</f:facet> 
    			<t:outputText value="#{genre.name}"/>
    		</t:column>
    	</t:dataTable>
    	</t:buffer> 
		<h:outputText value="#{export.htmlBuffer}" escape="false"/>		
    </h:form>
    </p>
</f:subview>

JSF Managed Bean

The above code makes a call to the following Bean.

test/Export.java

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

public class Export implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String htmlBuffer;
    
    
    public Export() {
        
    }
    
    public void exportHtmlTableToExcel() throws IOException{        

//        //Set the filename
//        DateTime dt = new DateTime();
//        DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd_HHmmss");
        String filename = ""+System.currentTimeMillis() + ".xls";
        
        
        //Setup the output
        String contentType = "application/vnd.ms-excel";
        FacesContext fc = FacesContext.getCurrentInstance();
        
        HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
        response.setHeader("Content-disposition", "attachment; filename=" + filename);
        response.setContentType(contentType);
        
        //Write the table back out
        PrintWriter out = response.getWriter();
        out.print(htmlBuffer);
        out.close();
        fc.responseComplete();
    }  
    
    public String getHtmlBuffer() {
        return htmlBuffer;
    }
    
    public void setHtmlBuffer(String htmlBuffer) {
        this.htmlBuffer = htmlBuffer;
    }

}



Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

February 2017
S M T W T F S
« Mar    
 1234
567891011
12131415161718
19202122232425
262728  

Blog Stats

  • 795,599 hits