DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Export to Excel, PDF, CSV and XML using Display tag

Export to Excel, PDF, CSV and XML using Display tag

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Tutorial
Like (1)
Save
Tweet
Share
140.88K Views

Join the DZone community and get the full member experience.

Join For Free

Using display tag library, we can export the data grid as excel, pdf, csv and xml. In the following example we will see how to dispaly data using display tag and to export as excel, pdf, csv and xml.

The following jar files should be placed in the WEB-INF/lib directory

  • antlr
  • commons-beanutils
  • commons-beanutils-1.7.0
  • commons-collections-3.1
  • commons-digester
  • commons-fileupload-1.0
  • commons-lang-2.3
  • commons-logging
  • commons-validator
  • displaytag-1.2
  • displaytag-export-poi-1.2
  • displaytag-portlet-1.2
  • itext-1.3
  • jakarta-oro
  • log4j-1.2.13
  • struts

itext-1.3.jar file is required incase of exporting to pdf. displaytag-export-poi-1.2.jar is requried to export the file as csv, xml and excel.The following taglib directive should be placed in each JSP page that uses the display tag.

<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

In this example we will display a list of actor's details like name, email Id and the TV show in which they performed. In this example we have a ActorData class which store the actor's details like name, email id and the tv show. The ActorData class has a loadData() method which returns an ArrayList of all the actor details.

The following attributes and methods are present in the ActorData class.

public class ActorData
{
  private String tvShow;
  private String userName;
  private String emailId;
  public ActorData()
  {

  }

  public ActorData(String tvShow, String userName, String emailId)
  {
      this.tvShow = tvShow;
      this.userName = userName;
      this.emailId = emailId;
  }
  public ArrayList loadData()
  {
      ArrayList userList = new ArrayList();
      userList.add(new ActorData("The Office","Michael Scott",
            "michael.scott@dundermifflin.com"));
      userList.add(new ActorData("The Office","Dwight Schrute",
            "dwight.schrute@dundermifflin.com"));
      userList.add(new ActorData("The Office","Jim Halpert",
            "jim.halpert@dundermifflin.com"));
      userList.add(new ActorData("The Office","Pam Beesly",
            "pam.beesly@dundermifflin.com"));
      userList.add(new ActorData("The Office","Andy Bernad",
            "andy.bernad@dundermifflin.com"));
      userList.add(new ActorData("The Office","Angela Martin",
            "angela.martin@dundermifflin.com"));
      userList.add(new ActorData("Friends","Rachel Green",
            "rachel.green@friends.com"));
      userList.add(new ActorData("Friends","Monica Geller",
            "monica.geller@friends.com"));
      userList.add(new ActorData("Friends","Phoebe Buffay",
            "phoebe.buffay@friends.com"));
      userList.add(new ActorData("Friends","Joey Tribbiani",
            "joey.tribbiani@friends.com"));
      userList.add(new ActorData("Friends","Chandler Bing",
            "chandler.bing@friends.com"));
      userList.add(new ActorData("Friends","Ross Geller",
            "ross.geller@friends.com"));
      return userList;
  }
  public String getTvShow() {
    return tvShow;
  }
  public String getUserName() {
    return userName;
  }
  public String getEmailId() {
    return emailId;
  }

}

In the execute() method of the UserAction class, the loadData() method of ActorData class is called. This method will return an ArrayList of actors, that ArrayList is stored in the actorList attribute of the UserForm class.

public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserForm userForm = (UserForm) form;
    ActorData actorData = new ActorData();
    userForm.setActorList(actorData.loadData());
    return mapping.findForward(SUCCESS);
}

Now we will display all the actor data present in the actorList in the jsp page using dispalytag.

<display:table id="data" name="sessionScope.UserForm.actorList" requestURI="/userAction.do" pagesize="10" >
<display:column property="tvShow" title="TV Show" sortable="true" media="html" group="1" />
<display:column property="userName" title="User Name" sortable="true"/>
<display:column property="emailId" title="Email Id" sortable="true"/>
</display:table>

The export attribute of the table tag should be set to true, inorder to export the data grid. After setting the value the data grid can be exported to excel, csv and xml. If you want to export to pdf then the "export.pdf" property to true.

<display:setProperty name="export.pdf" value="true" />

By default the name of the export file will be the name of the jsp page. You can change the name of the pdf file by setting the "export.pdf.filename" property value to the desired file name. In the similar way you can change the name of the excel file by setting "export.excel.filename" property to a desired file name.

<display:setProperty name="export.pdf.filename" value="ActorDetails.pdf"/>
<display:setProperty name="export.excel.filename" value="ActorDetails.xls"/>

The name attribute of the table tag hold the name of the list in the form. The id value specifies an instance name for the ArrayList. The pagesize attribute holds the number of records to be displayed in each page.

The property attribute of the column tag hold the value of the property to be displayed in this each column. The value of the property can be any one of the property of the ActorData class. The ActorData class should have a getter method for that corresponding property. For instance if the property name is tvShow then their should be a corresponding getTvShow() method in the ActorData class.

If the sortable attribute of the column tag is set to "true" then that column will be sortable.

The group attribute of the column tag is set to "1" , which means each unique data will be displayed only once and will not be repeated for the subsequent times.

The following image shows the first page of the data grid. Since the pagesize is set to ten, ten records are displayed in the first page.

On clicking the Excel link the user will be prompted to open or save the file.

On clicking open the data grid will be displayed in Excel format.

The media property of the column tag is used to specify in which media that column should be shown. If the media is set to "html" then only in the jsp page that particular column will be displayed. If the media is set to "html, excel" then that particular column will be displayed in jsp page as well as in excel.

<display:column property="tvShow" title="TV Show" sortable="true" media="html" group="1" />

On clicking the PDF link the user will be prompted to open or save the file.

On clicking open the data grid will be displayed in PDF format.

On clicking the CSV link the data grid will be displayed in the csv format.

On clicking the XML link the data grid will be displayed in the xml format.

You can download the source code of the displaytag export to excel example by clicking on the Download link below.

Source: Download

Source + Lib: Download

PDF XML CSV Data grid Database Data (computing) Property (programming) Attribute (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Stress Testing Tutorial: Comprehensive Guide With Best Practices
  • Microservices 101: Transactional Outbox and Inbox
  • Low-Code Development: The Future of Software Development
  • How To Use Java Event Listeners in Selenium WebDriver

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: