Mar 07

Displaytag is a great tag library for rendering data as a table. As well as handling the generation of the html table, it also provides support for easily exporting the table to a variety of formats including Excel (and CSV).

One problem that can occur is Excel formatting the table cells. When it sees a value that looks like a number then it attempts to format it as a number. This can result in values being displayed in Excel in scientific notation when you really want the data to be displayed as is.

To fix this we can use a displaytag column decorator that quotes the values when exporting to Excel. That way excel will see the quoted value and no longer think it as a number and display it without formatting as you require.

import javax.servlet.jsp.PageContext;
import org.displaytag.decorator.DisplaytagColumnDecorator;
import org.displaytag.properties.MediaTypeEnum;

public class QuotedExportDecorator implements DisplaytagColumnDecorator {
	
	@Override
    public Object decorate(Object value, PageContext pageContext, MediaTypeEnum media) {
        if (media.equals(MediaTypeEnum.EXCEL) || media.equals(MediaTypeEnum.CSV)) {
            value = "=\"" + value + "\"";
        }
        return value;
    }
}

written by objects \\ tags: , , , ,


One Response to “Displaytag exporting to excel”

  1. ishwarya Says:

    Great one..but after displaying in excel,is thr any way that I can remove the quotes

Leave a Reply

You must be logged in to post a comment.