Over a million developers have joined DZone.

Struts Export WorkBook to Excel Tutorial

DZone's Guide to

Struts Export WorkBook to Excel Tutorial

Free Resource

In this example we will see how to export a WorkBook to Excel. ExcelCreator class is used to create the HSSFWorkbook. The ExcelCreator class contains the following code.

public class ExcelCreator {

    public HSSFWorkbook createWorkbook(ArrayList userList) throws Exception {

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("User Data");

         * Setting the width of the first three columns.
        sheet.setColumnWidth(0, 3500);
        sheet.setColumnWidth(1, 7500);
        sheet.setColumnWidth(2, 5000);

         * Style for the header cells.
        HSSFCellStyle headerCellStyle = wb.createCellStyle();
        HSSFFont boldFont = wb.createFont();

        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue(new HSSFRichTextString("User Name"));
        cell = row.createCell(1);
        cell.setCellValue(new HSSFRichTextString("Email Id"));
        cell = row.createCell(2);
        cell.setCellValue(new HSSFRichTextString("Location"));

        for (int index = 1; index < userList.size(); index++) {
            row = sheet.createRow(index);
            cell = row.createCell(0);
            UserData userData = (UserData) userList.get(index);
            HSSFRichTextString userName = new HSSFRichTextString(userData.getUserName());
            cell = row.createCell(1);
            HSSFRichTextString emailId = new HSSFRichTextString(userData.getEmailId());
            cell = row.createCell(2);
            HSSFRichTextString location = new HSSFRichTextString(userData.getLocation());
        return wb;

The ExcelCreator class contains the createWorkbook method which takes an ArrayList as the argument and returns a HSSFWorkbook .The ArrayList contains a list of UserData. The ArrayList is iterated using a for loop and each row in the excel is created.

In our example the UserAction class extends DispatchAction. The UserAction class contains an exportExcel method, which is used to export the workbook to excel. The UserAction class contains the following code.

public class UserAction extends DispatchAction {

    private final static String SUCCESS = "success";

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;
        UserData userData = new UserData();
        return mapping.findForward(SUCCESS);

    public ActionForward exportExcel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;
        ExcelCreator excelCreator = new ExcelCreator();
        HSSFWorkbook workbook = excelCreator.createWorkbook(userForm.getUserList());
        response.setHeader("Content-Disposition", "attachment; filename=UserDetails.xls");
        ServletOutputStream out = response.getOutputStream();
        return mapping.findForward(SUCCESS);

Run the example. The following user details will be displayed.

On clicking the Excel link the workbook is exported to excel. The following excel sheet will be displayed.

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

Source: Download

Source + Lib: Download


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}