Sheet Listeners

For finer control over the content and formatting of the resultant spreadsheet sheets, JETT allows the user to create SheetListener objects that are notified every time an entire sheet is being been transformed, regardless of its contents. Any object that implements the SheetListener interface may be registered with the ExcelTransformer prior to transformation. The SheetListener interface contains two methods:

public boolean beforeSheetProcessed(SheetEvent event);
public void sheetProcessed(SheetEvent event);

Register a SheetListener with the ExcelTransformer:

transformer.addSheetListener(new MySheetListener());

When a Sheet is processed, JETT generates a SheetEvent and notifies all registered SheetListeners. A SheetListener can retrieve context information by retrieving properties of the SheetEvent. The beforeSheetProcessed method is called just prior to the sheet being processed. Return true to process the sheet as normal, and return false to prevent the processing of the sheet, which stops sheetProcessed from being called.

Sheet sheet = event.getSheet();
Map<String, Object> beans = event.getBeans();

The Sheet is a reference to the POI Sheet object representing the actual sheet in the spreadsheet. The Map object is a reference to the beans map used to process the sheet. Any SheetListener can use the Sheet object to gain access to the POI Workbook or any Rows or Cells on the Sheet object.


A SheetListener can be created to time the sheet processing. The beforeSheetProcessed method notes down the system time just prior to processing. The sheetProcessed method notes down the system time just after processing, determines the difference, and populates the result.

private long startNanos;

public boolean beforeSheetProcessed(SheetEvent event)
   startNanos = System.nanoTime();
   return true;

public void sheetProcessed(SheetEvent event)
   long endNanos = System.nanoTime();
   double seconds = (endNanos - startNanos) / 1000000000.0;
   Sheet sheet = event.getSheet();
   int lastRow = sheet.getLastRowNum();
   Row timingRow = sheet.createRow(lastRow + 1);
   Cell timingCell = timingRow.createCell(0);
   timingCell.setCellValue("Sheet processing time: " + String.format("%.3f", seconds) + " s");

Here is what the bottom of such a sheet would look like:

Sheet processing time: 0.323 s