OFBiz Tutorial – Implementing a Product CSV export

Jacopo CappellatoPublished: Updated:

At the end of the last post in our OFBiz tutorial series we have completed the final version of a simple product list screen completely based on OFBiz widgets.

The end result is this:

The final result

Our next goal, the subject of this post, is to provide a CSV (Comma Separated Value) export of the same list of products; we will do this by reusing as much as possible the artifacts already implemented for the html screen.

A CSV file is a widely used simple format for the exchange of data between different software systems; for example, Microsoft Excel can easily import a CSV file into a spreadsheet. From a technical point of view a CSV file is a plain text file where each line represents a row from a list of homogeneous data, and the fields are separated by the comma character.
CSV exports are supported out of the box by the OFBiz widgets.
In this excercise we will:

  1. add to the product list screen a link that will generate the CSV export
  2. add to the controller.xml file the request and view entries for the new CSV export screen
  3. implement the new screen definition for the CSV export screen; the screen will reuse the same form used by html screen

Adding a link element

We will add a link from the product list screen to invoke the product CSV export screen. The definition of a link element in a screen is easy:

the “container” element is not mandatory but we have used it in order to create an invisible region of the screen for the links (we will add a new link in our next post, for PDF exports)

  • the link element is defined in the same way we have defined the link field in the product list form in our last post

Controller entries for a CSV export screen…

The entries are very similar to the ones we have implemented in a previous post for the product list screen. There are just a couple of things to notice:

  • the request-map uri must match with the target of the link element in the screen (“ProductListExport”)
  • in the view-map, the type is now “screencsv” (instead of “screen”) because we have to invoke the “csv screen renderer”
  • in the view-map we have also set the content-type to “text/csv” as useful metadata information for the browser

Screen definition for CSV export screens

There isn’t anything special in a screen definition for a CSV export; the screen is defined in the same way of a normal screen, except for a couple of details:

The screen definition is mostly identical to the “product list screen”

  • in particular, the entity-condition element is the same and it is used to select the list of products for the export
  • the form that is included is the same of the “product list screen”: we will not have to re-implement it because the widget renderer will take care of rendering it into the proper output (CSV or html)
  • the “set” field operation, where we set “viewSize” to “10000”, is an easy way to “disable” pagination; we actually define the top limit of our product export to 10000 records here, but of course we can use a different value

Conclusion

The exercise is complete and we can test the product export by clicking the “CSV Export” link (there is no need to restart OFBiz).

Implementing the CSV export ended up being a trivial task because, thanks to the OFBiz widgets, we have reused most of the work we did for the html screen.

In the next tutorial post we will perform similar steps to implement the PDF version of the same screen.

– Jacopo

 

Jacopo Cappellato About Jacopo Cappellato
Jacopo Cappellato is VP of Technology at HotWax Media and has been involved with the OFBiz project since 2003. He is an OFBiz Project Committer and a member of both the OFBiz Project Management Committee and the Apache Software Foundation.

Comments 3

  1. Avatar for Jacopo Cappellato
  2. Avatar for Jacopo Cappellato

    Hi i’m trying to follow the steps in this article,and when i click on CSV Export
    and i open ProductListExport in an excel file i get this message .

    org.ofbiz.webapp.view.ViewHandlerException: Unknown handler type: screencsv

    i dont know why it is not working,if you know anything that can help me to resolve this pblm ,thank you

    regards

    1. Avatar for Jacopo Cappellato

      Hi Maria,

      thank you for visiting our blog. My guess is that you are running the tutorial using an older version of OFBiz, is this possible?
      Ideally you should run the tutorial using the trunk (latest revision).
      More specifically, the error is caused by a missing entry in the controller:

      I hope it helps,

      Jacopo

Leave a Reply

Your email address will not be published. Required fields are marked *