File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Export to Excel in JSF 1.2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Export to Excel in JSF 1.2" Watch "Export to Excel in JSF 1.2" New topic
Author

Export to Excel in JSF 1.2

Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Hi,

I need to do a plain dump of my UI screen(WYSIWG) into a an excel document.
We are using JSF 1.2 and unfortunately can't upgrade to next version.

Further I need assistance on two things
1) Any ready-to-use utilties (jsf tags) to dump the datatable as is?
2) The records can run into 100 thousand plus, so how to tackle the Excel max limit?

Thanks,
Hari
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

You shouldn't attempt to create Excel documents (or other non-HTML documents) using JSF. Use a servlet for that part of the app.

I'm confused about the "screen dump" part, though, since that's not a function in any version of JSF. For one this, JSF is server tech, and the browser window is a client.


Customer surveys are for companies who didn't pay proper attention to begin with.
Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Hi Tim,

My requirement here is export the data on the screen as is (datatable) to either a pdf or excel. ( that's the dump)
I was looking for any predefined tags in JSF core implementaion that could be used to acheive this.
I know a few other JSF implementations like PrimeFaces have this ready made. ie, primefaces provids a simple tag to be used inside their datatable to export data on a click..

Just wondering does sun RI of JSF has any such additional utilities otherwise I will have right my custom component( a servlet or somethign like that ) to achieve what is required. (We can't upgrade non-sun JSF implementations)

Thanks,
Hari
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

As a general rule, if you cannot do something using UI-related using straight HTML, you cannot do it with the JSF core.

It's really very silly - also counter-productive - when shops put arbitrary restrictions on what third-party support you can utilize. Especially since a large percentage of today's J2EE systems, both open-source and commercial, are themselves made from a multitude of third-party components.

It would be one thing if we were dealing with a $50,000/desktop licensed commercial product, but most of the popular JSF extension tagsets: RichFaces, PrimeFaces, Oracle's own ADF (in its incarnation as Apache Trinidad), and I think even IceFaces are not merely free - they're open-source, generally with a license that is more than sufficient for both public and private projects.

It is not a trivial task to re-create complex functionality from scratch. It costs a lot of time and money to design and implement, and more time and money to support. Unless there's a compelling business advantage, you're going to come out behind when you set artificial limitations on what tools you can use.

Excel is most definitely NOT "write once, run anywhere", so you can forget ever seeing it natively supported in Java or core JSF unless Oracle's attitude shifts radically from Sun's. Failing that, and forbidden the economical way out, you have limited options.

1. You can create a custom binary component just like PrimeFaces did. As I said, this is horribly expensive, and when you build JSF components at the binary level you are at a higher-than-normal risk level for breakage when JSF3 comes out. Or maybe even JSF2.5. That stuff is extremely unstable.

2. You can create a servlet to output the Excel binary (or XML) file format. A servlet can access a JSF backing bean, so you could attach the existing display tablemodel to the servlet and use its getWrappedData() method to obtain the row data. Of course, the best way to output Excel format in a hurry is to use a - you probably guessed it - third-party software package such as Apache POI.

3. You can do a variant of #2 where the output is in CSV format. Very quick, but very dirty.
Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Hi Tim,

I am back again with the excel dump question.

I went ahead (inspite of your input of not doing this) and interceptd JSF response and put the xls stream in it.
The code works fine and I am able to Save the xls to the disk. However , I am unable to 'Open' the spread sheet directly from the open dialog.

On clicking Open from the dialog, the window refreshes with "Internet explorer cannot find the page"



I will for sure go the custom servlet as last option , but before that thought if I could tweak this .

Please advise.

Thanks,
Hari
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21


To a small child with a hammer, every problem looks like a nail.


I wasn't being arbitrary when I said not to use JSF, nor does the fact that you didn't get an Exception mean that you produced a valid Excel document.

JSF backing beans are not servlets, and the processing of JSF backing beans is not servlet-like. In the Model/View/Controller paradigm, JSF backing Beans are Models. The Controller (FacesServlet) uses the Model(s) as a source for data that it renders, but JSF expects to do all the rendering itself, using the View definition as a template. So while you were writing out-of-sync information defining the response stream as Excel, JSF was busy outputting data that defined it as HTML. Small wonder that the client couldn't read the result.

JSF is not a greedy framework that has to be 100% in control for each and every thing that the application does. It's a means of working with and navigating between web pages, with an emphasis on forms and validation. In cases where you need something that's not HTML, such as producing PDF's, Word documents, and Excel spreadsheets, you should use a tool that's better fitted to the task. Which, in the case of Excel files, would be a servlet.
Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Thanks Tim. That clearly explains it.
I had to the dirty fix becayse of my time and effort constraints.

That being said, my requirement still would clicking button on jsf page and magically a dialog box with xls stream appears and the jsf page aslo stays the way it is.

Assuming my servlet the responds with a xls stream is ready, how and where do I fit it in to meet my requirement.

Pardon my ignorance.

Thanks in Advance.

Hari
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Place a "h:outputLink" tag on the page that selects for creation of the Excel document and set its value to the URL of the servlet that generates the document.
Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Thanks Tim.

But doesn't hutputlink completely redirect the request to a different url?
I need my jsf page to remain intact and also get the servlet response.

Thanks,
Hari
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Try it. Normally, it would, but I believe the nature of the output (Excel instead of HTML) will prevent that, since Internet Explorer is forbidden by law to open Excel documents in the browser. To be on the safe side, however, you should set the content-disposition header for the benefit of non-Windows users.

If you do have problems, just set the "target" attribute in the outputlink to output to a different place.
Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Thanks Tim.

I am trying this as I am typing here, and the "hutputlink" to another servlet on WAS 6 is giving me a 404.
I have all my web.xml mapping right. I am completely lost here.

Anything missing?

Thanks,
Hari
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

The web.xml file needs to have the same type of servlet mapping for your Excel-generator servlet as it would for any non-JSF servlet. However, check the URL. JSF already knows about the webapp context, so if you code an outputlink to "/mywebapp/excelservlet" and the webapp context is "/mywebapp", JSF will generate 'href="/mywebapp/mywebapp/excelservlet". You only need to code "/excelservlet" in the JSF outputLink.
Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Hi Tim,

Few more problems do this the cleaner way

Problem 1:
The 404 is still present even if I tried the way you suggested.

Problem 2:
I have to pass the data object to the servlet for the export.
I tried using the f:param but it submits the param as a string and not as objects I require.

I have this in my code

Is there a way I can set them into session?
Please let me know how can pass the object?

Thanks,
Hari
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

The 404 problem is just basic web stuff. You need to check the generated URL on the webpage against your servlet mapping in web.xml.

It's very easy to pass information from JSF to servlets/JSPs and back again. JSF session-scope backing beans are literally J2EE session attribute objects. There's no magic to them. The only difference between JSF session scope objects and servlet session scope objects is that JSF will automatically construct the object on demand without user application code, and JSF will wire it into other JSF backing beans as directed by faces-config.xml. The actual session scope object is just a POJO, so the only restriction is that it shouldn't reference the FacesContext. That's because the FacesContext is created by the FacesServlet and destroyed after each use and therefore won't exist when any other servlet except the FacesServlet is operating.
Hari Gundappa
Greenhorn

Joined: Sep 13, 2011
Posts: 27
Thanks Tim.

I have put in a servlet and kept the JSFpart clean.

Thanks again.

Hari
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Export to Excel in JSF 1.2