I am using display tag in my web application. I am successfully able to export the data displayed by display tag to excel.
The problem is I also want the header and data row styles and css to be included in the exported excel.
e.g. Header row is bold with gray background and columns in data rows are colored depending on the value.
But this is not getting exported to excel.
EDIT 1:-
Below is the display tag code in my JSP. The list is shown properly with all the css applied to headers and data rows properly.
I can also export the data into the excel.
<display:table name="userList" pagesize="20" class="listingTable" keepStatus="true"
cellpadding="0px" cellspacing="0px" id="user" export='true' requestURI="">
<display:setProperty name="export.decorated" value="true" />
<display:setProperty name="export.excel.filename" value="User List.xls" />
<display:column titleKey="user.firstname" property="firstname"></display:column>
<display:column titleKey="user.lastname" property="lastname"></display:column>
<display:column titleKey="user.email" property="email"></display:column>
<display:setProperty name="paging.banner.item_name" value="User" />
<display:setProperty name="paging.banner.items_name" value="Users" />
I am using the default display tag style sheet with some minor changes to it.
Please help.
From my recent learning what i have understood that Display tag cannot directly export css style used in HTML. You have to do the formatting and styling programatically
I have taken an approach and i am successful.
The procedure is:
you will need to create a class to replace the default export view class of the display tag as like
ExcelHssfView
orDefaultHssfExportView
you can do this by implementing
ExportView
interface as display tag documentation says:Any valid class that implements the org.displaytag.export.ExportView interface.
But i implemented
BinaryExportView
interface as it extendsExportView
so its all the same. Truly speaking i just copied all the code from the source ofExcelHssfView
and edited only thedoExport
method. if you look into the source you will understand what will you have to do. hints :useHSSFCellStyle
to give any style to the excel fileand then point you class in the
.property file
.export.excel.class=youpackage.yourExlcelView
see example of a
ExportView
:ExcelHssfView Source CodeCSS Styles must be directly applied to each element that needs styling. So for a bold header row with a grey background, you need the following css:
th { background-color: grey-25-percent; font-weight: bold }
(or instead of
th
, usethead
orthead tr
)displaytag also applies some classes to odd/even rows, sorted rows, and a few others. See this page for more details.
Also, Excel can only use 56 different colors, so if you want a font color or background color, you must use a color that Excel can accept. See this article for the full range of Excel compatable colors.
Here is a short list of acceptable colors: AQUA, BLACK, BLUE, BLUE_GREY, BRIGHT_GREEN, BROWN, CORAL, CORNFLOWER_BLUE, DARK_BLUE, DARK_GREEN, DARK_RED, DARK_TEAL, DARK_YELLOW, GOLD, GREEN, GREY_25_PERCENT, GREY_40_PERCENT, GREY_50_PERCENT, GREY_80_PERCENT, INDIGO, LAVENDER, LEMON_CHIFFON, LIGHT_BLUE, LIGHT_CORNFLOWER_BLUE, LIGHT_GREEN, LIGHT_ORANGE, LIGHT_TURQUOISE, LIGHT_YELLOW, LIME, MAROON, OLIVE_GREEN, ORANGE, ORCHID, PALE_BLUE, PINK, PLUM, RED, ROSE, ROYAL_BLUE, SEA_GREEN, SKY_BLUE, TAN, TEAL, TURQUOISE, VIOLET, WHITE, YELLOW
To export a HTML table with styling information you cannot use Class method of CSS. Instead in each of the HTML tags that need to be formatted in some manner you have add Style parameters. For Example to set style for Table Row use