diff --git a/modules/gui/src/com/haulmont/cuba/gui/components/actions/ExcelAction.java b/modules/gui/src/com/haulmont/cuba/gui/components/actions/ExcelAction.java index c9a3b8bdd7..4d182a2dd3 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/components/actions/ExcelAction.java +++ b/modules/gui/src/com/haulmont/cuba/gui/components/actions/ExcelAction.java @@ -16,6 +16,7 @@ */ package com.haulmont.cuba.gui.components.actions; +import com.haulmont.cuba.core.entity.Entity; import com.haulmont.cuba.core.global.AppBeans; import com.haulmont.cuba.gui.AppConfig; import com.haulmont.cuba.gui.components.*; @@ -51,6 +52,7 @@ public class ExcelAction extends BaseAction implements Action.HasBeforeActionPer protected final ListComponent listComponent; protected final ExportDisplay display; + protected String fileName = null; protected BeforeActionPerformedHandler beforeActionPerformedHandler; @@ -148,6 +150,20 @@ public class ExcelAction extends BaseAction implements Action.HasBeforeActionPer this.icon = thCM.getThemeValue("actions.Excel.icon"); } + /** + * @return excel file name without extension + */ + public String getFileName() { + return fileName; + } + + /** + * @param fileName excel file name without extension + */ + public void setFileName(String fileName) { + this.fileName = fileName; + } + /** * This method is invoked by action owner component. * @param component component invoking action @@ -197,16 +213,16 @@ public class ExcelAction extends BaseAction implements Action.HasBeforeActionPer protected void export(ExportMode exportMode) { ExcelExporter exporter = new ExcelExporter(); if (listComponent instanceof Table) { - Table table = (Table) listComponent; - exporter.exportTable(table, table.getNotCollapsedColumns(), display, exportMode); + Table table = (Table) listComponent; + exporter.exportTable(table, table.getNotCollapsedColumns(), false, display, null, fileName, exportMode); } if (listComponent instanceof DataGrid) { - DataGrid dataGrid = (DataGrid) listComponent; + DataGrid dataGrid = (DataGrid) listComponent; List columns = dataGrid.getVisibleColumns().stream() .filter(col -> !col.isCollapsed()) .collect(Collectors.toList()); - exporter.exportDataGrid(dataGrid, columns, display, exportMode); + exporter.exportDataGrid(dataGrid, columns, display, null, fileName, exportMode); } } diff --git a/modules/gui/src/com/haulmont/cuba/gui/export/ExcelExporter.java b/modules/gui/src/com/haulmont/cuba/gui/export/ExcelExporter.java index 7ac8bad4c3..7a5c0a1ca5 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/export/ExcelExporter.java +++ b/modules/gui/src/com/haulmont/cuba/gui/export/ExcelExporter.java @@ -44,6 +44,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.text.ParseException; import java.util.*; +import java.util.stream.Collectors; /** * Use this class to export {@link com.haulmont.cuba.gui.components.Table} into Excel format @@ -197,7 +198,11 @@ public class ExcelExporter { CollectionDatasource datasource = table.getDatasource(); if (exportMode == ExportMode.SELECTED_ROWS && table.getSelected().size() > 0) { - for (Entity item : table.getSelected()) { + Set selected = table.getSelected(); + List ordered = ((Collection) datasource.getItems()).stream() + .filter(selected::contains) + .collect(Collectors.toList()); + for (Entity item : ordered) { createRow(table, columns, 0, ++r, item.getId()); } } else { @@ -327,7 +332,11 @@ public class ExcelExporter { CollectionDatasource datasource = dataGrid.getDatasource(); if (exportMode == ExportMode.SELECTED_ROWS && dataGrid.getSelected().size() > 0) { - for (Entity item : dataGrid.getSelected()) { + Set selected = dataGrid.getSelected(); + List ordered = ((Collection) datasource.getItems()).stream() + .filter(selected::contains) + .collect(Collectors.toList()); + for (Entity item : ordered) { createDataGridRow(dataGrid, columns, 0, ++r, item.getId()); } } else {