Struts 2 XLS Download

Struts 2 XLS Download

In report creation series today you will see how to generate XLS report in Struts 2 application. Based on client request server will generate XLS file and attach to the response where client will be able to view/download report in XLS format.

Please see previous tutorial to create report in other format:

  • Create maven project name: Struts2XLSDownload in eclipse. We are using eclipse kepler which comes with built-in maven plug-in to create maven project. Below is final project structure:

2015-01-18_0157

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javahonk</groupId>
	<artifactId>Struts2XLSDownload</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Struts2XLSDownload Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.16.3</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-convention-plugin</artifactId>
			<version>2.3.16.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>		
	</dependencies>
	<build>
		<finalName>Struts2XLSDownload</finalName>
	</build>
</project>
  • ApplicationResources_en.properties
label.welcome = Struts 2 XLS Download Example
  • Struts2XLSDownloadAction.java:
package com.javahonk.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;


public class Struts2XLSDownloadAction{

	@Action(value = "/downloadXLSFileAction")
	public void downloadCSVFileAction() throws Exception {

	    HttpServletResponse response = ServletActionContext.getResponse();
	    try {
			response.setContentType("application/vnd.ms-excel");
			String reportName = "JavaHonk_Struts2_XLS_Report_.xls";
			response.setHeader("Content-disposition", "attachment; filename=" + reportName);
			ArrayList<String> rows = new ArrayList<String>();
			rows.add("First Name");
			rows.add("\t");
			rows.add("Last name");
			rows.add("\t");
			rows.add("Test");
			rows.add("\n");
 
			for (int i = 0; i < 5; i++) {
				rows.add("Java");
				rows.add("\t");
				rows.add("Honk");
				rows.add("\t");
				rows.add("Success");
				rows.add("\n");
			}
			Iterator<String> iter = rows.iterator();
			while (iter.hasNext()) {
				String outputString = (String) iter.next();
				response.getOutputStream().print(outputString);
			}
			
			response.getOutputStream().flush();
 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
  • index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Struts 2 XLS Download Example</title>
<s:head />
</head>

<body>
	<h2 style="color: green">
		<s:text name="label.welcome" />
	</h2>
	<s:form method="post" action="downloadXLSFileAction">
		<s:submit value="Download XLS" />
	</s:form>
</body>
</html>
  • You could run this project on any server below will be output:

2015-01-18_0201 2015-01-18_0202 2015-01-18_0203

  • Fork your own copy from GitHub

download  Download Project: Struts2XLSDownload 

Leave a Reply

Your email address will not be published. Required fields are marked *