1
votes

I working with a cell table and im supporting filtering operations on the client. I was looking for a way to download the table to excel directly from the gwt client. Is there an easy way to do this?

I found http://code.google.com/p/gwt-table-to-excel/. But I was looking for a way to do it without using the above.

4

4 Answers

3
votes

The class below do that without server side.

public class TableToExcel {
    public static final <T> void save(final CellTable<T> table, String filename) {
        final AnchorElement a = Document.get().createAnchorElement();
        a.setHref("data:application/vnd.ms-excel;base64," + base64(table.getElement().getString()));
        a.setPropertyString("download", (filename.endsWith(".xls") || filename.endsWith(".xlsx")) ? filename : filename + ".xls");

        Document.get().getBody().appendChild(a);
        Scheduler.get().scheduleEntry(new ScheduledCommand() {
            @Override
            public void execute() {
                click(a);
                a.removeFromParent();
            }
        });
    }

    private static native void click(Element elem) /*-{
        elem.click();
    }-*/;

    public static native String base64(String data) /*-{
        return btoa(data);
    }-*/;
}
0
votes

I found a suitable solution using http://en.wikipedia.org/wiki/Data_URI_scheme

I did not want to go back to the server, because I have everything on the client !

This allows for really quick download of the cell tables generated.

( It does not scale beyond a certain point , but that is acceptable for my use case. Speed of download takes preference. )

0
votes

Suppose if you are generating reports from 01/01/2014 to 31/12/2014 You need to pass parameters to the server through from client side such as :

                String parameters = "mode=" + report  + "&frmDate=" + from + "&toDate="+ upto ;
                String url = URL.encode(GWT.getHostPageBaseURL() + "something/something?" + parameters);
                // window for download process Excel shown to user
                String features = "menubar=no,location=no,status=no,width=200,height=100,toolbar=no,scrollbars=no,resizable=no";
                //pass the feature parameter to Window.open...
                // since GWT has provided this method -        public static native void open(String url, String name, String features)
                com.google.gwt.user.client.Window.open(url, "_blank", features); 
-2
votes

If you want to download a Excel File, you must write the creation from the server. You must send yours filterings operations to the server to tell hime to generate the file.

The projet gwt-table-to-excell use a hack from Excel which can try to read html table for build the excel file. But this may not work in a new version.