Writing a table to an excel file

103 views
Skip to first unread message

ewtsm

unread,
Mar 26, 2007, 4:38:21 PM3/26/07
to Google Web Toolkit
I've been experimenting with GWT, and I need to be able to take a
table (preferably a FlexTable) and write it to an excel file, which I
can then make available for download on the client side. Is there any
easy way to do this?

Thanks.

map

unread,
Mar 26, 2007, 5:34:39 PM3/26/07
to Google Web Toolkit
I don't know if there's an easy way, but I found a hard way ;-)

There's no way (that I know) of getting the browser the pop up the
download popup except as a result of an HTTPResponse. So if you want
to grab the data out of the FlexTable on the client side, you've got
to post the data to the server (such as via a GWT Service), then
create a new HttpRequest that gets the data from the server and sets
the MIME type to CSV so the browser opens the download popup.

You might want to consider just calling the server for the data in the
first place and forget about getting it out of the FlexTable. That
would be simpler, but then you lose any formatting the user has done,
such as client-side sorting of the table.

Info on the CSV format can be found here -
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#FileFormat

Here's the awkward sequence of events I had to go through:
1. Iterate through the table and accumulate the cell values (in CSV
format) in a StringBuffer.
2. Push the String to the server via a GWT service, which puts the
data in the HttpSession.
3. In the service callback's onSuccess() method, call a servlet (I
used Window.open(url, "_self", "");) that retrieves the data from the
HttpSession, sets the HttpResponse MIME type to text/csv, and streams
the data back on the HttpResponse. Don't forget to remove the data
from the HttpSession ;-)

Hope this helps.

Michael

Brill

unread,
Mar 27, 2007, 1:26:32 AM3/27/07
to Google Web Toolkit
Jakarta POI - Java API To Access Microsoft Format Files
http://jakarta.apache.org/poi/index.html

- Brill Pappin

On Mar 26, 4:38 pm, "ewtsm" <miller.elli...@gmail.com> wrote:

Maarten Volders

unread,
Mar 27, 2007, 3:36:14 AM3/27/07
to Google-We...@googlegroups.com
Jakarta POI is really nice for things like that.
You can:

1. Generate all the POI stuff async on the server
2. on completion just store (temporary) the data created on file or in memory
3. return some kind of reference key to the client informing him the processing has finished
4. open a new browser popup which includes the reference key, let him go with that key go to the server,
5. some server controller can then just lookup your POI stuff up in memory or on file using the reference key
6. return the data back on the http response, et voila, async excel file generation, or at least it seems to be working in my head; never tried it before :-)

Hope it helps

Gtz

M.

Get!em

unread,
Mar 27, 2007, 4:51:13 AM3/27/07
to Google Web Toolkit
All nice answers, but there are 2 really simple solutions:

1st
Generate a html table and save the file with a .xls extension and send
that to the client. It really works.

2nd
Jasper reports. Generate a report and export it to many formats,
including pdf, xls and csv
http://www.jasperforge.org/

greetings

Get!em

Marcelo Emanoel

unread,
Mar 27, 2007, 8:54:36 AM3/27/07
to Google Web Toolkit
I agree with the poi solution...
but I've made it even simpler :)

write a plain old servlet, override the service method, change the
content-type to "application-x/xls" and write in the
response.getOutputStream.
Since you are on the server, make the search again... its faster...
map the servlet you've made to a pattern like this "*.xls" so whatever
you the servlet will end up like a xls file and the client will think
you have generated a excel file for him but you don't need to worry
about files its just the response... :)
thats it :)
hope it works for you :)

On Mar 27, 5:51 am, "Get!em" <Get...@gmail.com> wrote:
> All nice answers, but there are 2 really simple solutions:
>
> 1st
> Generate a html table and save the file with a .xls extension and send
> that to the client. It really works.
>
> 2nd
> Jasper reports. Generate a report and export it to many formats,

> including pdf, xls and csvhttp://www.jasperforge.org/

Maarten Volders

unread,
Mar 27, 2007, 9:03:48 AM3/27/07
to Google-We...@googlegroups.com
The point I wanted to make with offline / async generation of reports is that you add extra convenience to to client. In plain old page reload application the user would have to wait for the response, or having a second browser window open which is waiting for the server response. With async communication you can really inform the user when the report is ready and give him the possibility to choose when he wants to see the report ... just think about it ;-)

ewtsm

unread,
Mar 27, 2007, 12:41:19 PM3/27/07
to Google Web Toolkit
Thanks to all of you. I'll try out a couple of these solutions. This
is exactly what I was looking for.

-ewtsm

On Mar 27, 6:03 am, "Maarten Volders" <maarten.vold...@gmail.com>
wrote:


> The point I wanted to make with offline / async generation of reports is
> that you add extra convenience to to client. In plain old page reload
> application the user would have to wait for the response, or having a second
> browser window open which is waiting for the server response. With async
> communication you can really inform the user when the report is ready and
> give him the possibility to choose when he wants to see the report ... just
> think about it ;-)
>

Reply all
Reply to author
Forward
0 new messages