Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Table to Excel and PDF  RSS feed

 
Vineeth Vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have something like the below in my JSP page. It is basically a table that is generated from an ArrayList called myArrayList. I am getting this as a request Attribute from the servlet. My question is : How can I convert this into excel and pdf. Any help would be appreciated. The other condition is that. I will only be populating 10 rows of the arraylist for the user display but would want to export the whole arraylist into excel or pdf. . Please help

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in a JSP, or with anything to do with JSP.

Get the data in a servlet or other Java class in the same way that you are getting it for the JSP and stream the converted data as the response. You likely want to use a 3rd-party library to do the conversion. Be sure to set the response headers appropriately.

At least, I assume you want to serve it to the browser -- right?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Why are you still using Java scriptlets in a JSP 14 years after they have been obsoleted?
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most probably this should be done server side.

How do you limit the number of rows in it right now?
You have to change that to be conditional based on the format required, so you will need to be modifying your servlet.

For converting to PDF, you can use the library: Flying Saucer
For Excel, one quick hack is just to set the content type to application/vnd.ms-excel and just seeing what happens.


 
Vineeth Vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies

1) Yes, I should be not using scriptlets anymore

2) How can I use display tags for the same? I believe that has some export capabilities?

3) I am looping over the array list to limit the count
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to export native Excel format, or something stop-gap like CSV (or an HTML table)?
 
Vineeth Vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
xls/xlsx

and pdf
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, then you'll need a library, such as FOP, for the Excel. There are a number of PDF libraries around; Stefan has pointed you to one of them.

You would not use JSP for this; stream the data from a servlet.

 
Vineeth Vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks

another basic question. If I want to send this ArrayList, which is quite big back, to the servlet through an actor tag click how would it work? URL param? The array list is really big
 
Vineeth Vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean anchor tag <a href>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't do it that way at all -- it makes no sense. Rather, fetch the data from the database the same way that it was originally fetched. The array list is long gone by the time the prate appears in the browser.
 
Vineeth Vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSP page has a form with lot of drop down lists and when the page is submitted I get 2-3 tables. Now against each table I need to have an export to excel and export to PDF option. If I have to convert the table to excel or pdf I need to pass the array list back to the servlet right?

or how do I manage it?
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the information hasn't been edited by the user, I would just abandon the data in the JSP, re-run the query and pipe the data to the spreadsheet/pdf rather than the JSP.
There is no point sending to the server data that it already has, if all it is going to do is send it back again.
Particularly if that table is only a subset of the full data that you need to download in the spreadsheet/pdf.



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Stefan said. The browser page is an end point, not a data source. Your server-side code should, as I have already said, just fetch the data again from wherever it comes from.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!