Tuesday, May 5, 2009

Force Displaytag Export to Text

When I was using displaytag to export to excel, one of my columns contained text which was being recognized as numeric (scientific notation). When googling for a possible solution, seems like a LOT of people have had this problem in various ways (displaytag or otherwise). Thanks to this webpage, my solution was to write the following column decorator:

Custom Decorator

request.setAttribute("yourdecorator", new DisplaytagColumnDecorator() {
public Object decorate(Object o, PageContext pageContext, MediaTypeEnum media) {
String decorated = (String)o;
if (media.equals(MediaTypeEnum.EXCEL) || media.equals(MediaTypeEnum.CSV)) {
decorated= "=\"" + packet + "\"";

return decorated

<display:column title="your-title" media="xml excel csv" property="your-property" decorator="yourdecorator" />

I have not run into any issues with this approach yet. If you do, I would appreciate a comment back. Thanks again to the guy who posted the hint that I reference previously.

p.s. Someone also suggested that I prepend the text with a unicode non-breaking space (see code following). Visually, it has the desired effect but it also modifies the text in the field so I do not like the solution as well. I just offer that solution to those who might want to use it.

Custom Decorator

decorated= "\u00a0" + packet;

No comments: