Spring : Generate an excel document as a Spring view

tastelessbeachInternet and Web Development

Nov 12, 2013 (3 years and 10 months ago)

70 views

Spring : Generate an excel document as a
Spring view


This article’s
goal

is to show you to simply generate a report readable with Excel in Java
technology using Spring MVC
Framework
.

1) Some
details:

Spring Framework (http://www.springframework.org) gives to the user the tools for PDF, MS
Words, MS Excel, etc documents creatio
n. As Spring is a clean MVC Framework, the
creation of an Excel document is done by
creating a Controller and a View which extends
AbstractExcelView
.

Here is a code sample

2) The controller
…:


Here is a Spring controller sample really easy, which will
just add to the model a test value,
usefull for the view rendering mechanism validation.



Path

: com.tellaw.test.controllers

package

com.
tellaw
.
test
.
controllers
;



import java.util.*;

import javax.servlet.http.*;



import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.web.servlet.ModelAndView;



@SuppressWarnings
(
"unchecked"
)

public

class

ExcelController
extends

AbstractController

{




protected

final

Log logger = LogFactory.
getLog
(
getClass
());




public

ModelAndView handleRequest
(
HttpServletRequest request,
HttpServletResponse response
)

throws

Exception

{





Map
<
String, Object
>

model =
new

HashMap
<
String, Object
>();


model.
put

(
"testValue"
,
"Test String for Excel View !!!"
);


return

new

ModelAndView
(
"excel
-
view"
,
"model"
, model
);




}



}


3) An excel view:


Here is the view, which will format and render the MS Excel Document. The view is based on
the POI Apache API, making as easy as possible the work on MS files format (Excel,
word,
Powerpoint) in Java techologies.



Path

: com.tellaw.test.controllers

package

com.
tellaw
.
test
.
view
;



import java.util.*;



import javax.servlet.http.*;



import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import
org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import
org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.hssf.util.Region;

import org.springframework.web.servlet.view.document.AbstractExcelView;



@SuppressWarnings
(
"unchecked"
)

public

class

ExcelExportView
extends

AbstractExcelView
{




protected

f
inal

Log logger = LogFactory.
getLog
(
getClass
());


private

static

String

SHEET_NAME =
"sheet"
;




protected

void

buildExcelDocument
(


Map

model,









HSSFWorkbook workbook,









HttpServletRequest
request,









HttpServletResponse
response
)

{




model =
(
Map
<
String, Object
>)
model.
get
(
"model"
);




// Get Active Datas


workbook = addSkeleton
(

workbook
);





// Define Style



HSSFCellStyle style = workbook.
createCellStyle
();


HSSF
Font boldFont = workbook.
createFont
();


boldFont.
setBoldweight
(
HSSFFont.
BOLDWEIGHT_NORMAL
);


style.
setFillForegroundColor
(

HSSFColor.
LIGHT_TURQUOISE
.
index

);


style.
setFillPattern
(
HSSFCellStyle.
SOLID_FOREGROUND
);


style.
setFont
(
boldFont
);





// Define headers


cell = getCell
(

sheet,
1
,
1

);


cell.
setCellStyle
(

style
);




// Number



setText
(

cell , model.
get
(
"testValue"
)

);




}




private

HSSFWorkbook addSkeleton
(

HSSFWorkbook workbook
)

{





HSSFSheet sheet
;


HSSFCell cell
;





sheet = workbook.
createSheet
(

ExcelExportView.
SHHET_NAME

);





// Define Style



HSSFCellStyle style = workbook.
createCellStyle
();


HSSFFont boldFont = workbook.
createFont
();


boldFont.
setBoldweight
(
HSSFFont.
BOLDWEIGHT_BOLD
);


style.
setFont
(
boldFont
);





// Define headers


cell = getCell
(

sheet,
3
,
1

);


cell.
setCellStyle
(
style
);



setText
(

cell ,
"Model test value is ?"

);





autoSizeCol
s
(

sheet
);





return

workbook
;


}



}


4) Configuring Spring context :


We just have now to do the wiring Spring stuff…


Path

: WEB
-
INF/springapp
-
servlet.xml (only for my test application)

<?xml

version
=
"1.0"

encoding
=
"UTF
-
8"
?>

<beans

xmlns
=
"http://www.springframework.org/schema/beans"



xmlns:p
=
"http://www.springframework.org/schema/p"



xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema
-
instance"



xmlns:aop
=
"http://www.springframework.org/schema/aop"



xmlns:tx
=
"http://www.sprin
gframework.org/schema/tx"



xmlns:context
=
"http://www.springframework.org/schema/context"



xsi:schemaLocation
=
"




http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring
-
beans
-
2.5.xsd




http
://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring
-
tx
-
2.5.xsd




http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring
-
context
-
2.5.xsd




http://www.springframework.
org/schema/aop
http://www.springframework.org/schema/aop/spring
-
aop
-
2.5.xsd"
>




<!
--




Abstract Controller



--
>


<bean

id
=
"abstractController"

abstract
=
"true"

class
=
"com.tellaw.test.controllers.AbstractController"
/>




<!
--

EXCEL Export controller
--
>


<bean


id
=
"excelView"





class
=
"com.tellaw.test.controllers.ExcelExportView"
>


</bean>




<bean


id
=
"excelController"





class
=
"com.tellaw.test.controllers.ExcelController"




parent
=
"abstractController"
/>




<!
--




Url Mapping
Definition With Authentication



--
>


<bean

id
=
"urlMapping"

class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>




<property

name
=
"mappings"
>


<props>


<prop

key
=
"/report.xls"
>
excelController
</prop>


</props>


</property>


</bean>




<!
--




View Resolver



--
>


<bean

class
=
"org.springframework.web.servlet.view.ResourceBundleViewResolver"
>


<property

name
=
"basename"

value
=
"views"
/>


</bean>





</beans>


5) Mapping a
view in a properties file :


I like to put the mapping of view in an external Properties file, so here is my file :

Path

: ressources/views.properties (Only for my test and my maven configuration)

excel
-
view.(class)
=com.tellaw.test.controllers.ExcelExportView

5) What’s next ????



Spring Framework :
http://www.springframework.org



Apache POI (Apache POI
-

Java API To Access Microsoft Format Files):
http://poi.apache.org/