How can I programmatically handle the Excel file generated by ExcelAction?

  • I want to generate an Excel file based on the current view’s filters (the ones set on the table UI).

  • Then, instead of offering it as a download to the user, I want to:
    Example:

    • Rename the file (optional),
    • Attach it to an email,
    • And send it to the user’s email address.

The problem is — I don’t know how to catch or access the generated file from ExcelAction or reuse its logic in a way that gives me the actual File object.

I think it is possible, with some custom code.

First, you should read code of com.haulmont.cuba.gui.actions.list.ExcelAction and com.haulmont.cuba.gui.export.ExcelExporter classes, to understand what’s going on there.

Then:

  • Create your own implementation of com.haulmont.cuba.gui.export.ExportDisplay interface. It should contain the logic of attaching the file to email etc. It can be a regular class created on the fly as needed. One of methods of this class will be called from ExcelExporter when the exported xls file is ready.

  • Create instance of ExcelExporter and call one of exportTable or exportDataGrid methods, passing instance of custom export display there.

ExcelExporter exporter = new ExcelExporter(ExcelExportFormat.DEFAULT);
ExportDisplay display = new MySendingToEmailExportDisplayImpl(emailAddress);
exporter.exportTable(table, table.getNotCollapsedColumns(), false, 
          display, null, fileName, ExportMode#ALL_ROWS);