在Vaadin 14中使用Excel导出网格

时间:2019-07-15 07:41:24

标签: java excel vaadin vaadin-grid vaadin-flow

我在vaadin中创建了网格。现在,我需要在Excel中导出该网格。我正在使用Vaadin Exporter,但无法使用Exporter导出网格。

Grid<Transaction> grid = new Grid<>(Transaction.class);
Button exportInExcel = new Button("Export in excel", event -> exportInExcel());

private void exportInExcel() {
    new Anchor(new StreamResource("my-excel.xlsx", Exporter.exportAsExcel(grid)), "Download As Excel");
}

如何在vaadin 14中的excel中导出网格

1 个答案:

答案 0 :(得分:0)

对我有用:

@Route(value = "erwachsenentraining", layout = MainLayout.class)
@PageTitle(Constants.ERWACHSENENTRAINING)
@Secured(Role.ADMIN)
public class ErwachsenenTraining extends VerticalLayout {

    private ErwachseneRepository erwachseneRepository;
    Grid<Erwachsene> grid;

    public ErwachsenenTraining(ErwachseneRepository erwachseneRepository) {
        this.erwachseneRepository = erwachseneRepository;

        grid = new Grid();
        ListDataProvider<Erwachsene> dataProvider = DataProvider.ofCollection(erwachseneRepository.findAll());
        grid.setDataProvider(dataProvider);
        grid.addColumn(Erwachsene::getEmail).setKey("email").setWidth("270px").setHeader("Email").setFlexGrow(5);
        grid.addColumn(u -> u.getFirstName() + " " + u.getLastName()).setKey("firstName").setHeader("Name").setWidth("200px").setFlexGrow(5);
        grid.addColumn(Erwachsene::getRole).setKey("role").setHeader("Role").setWidth("150px");

        add(grid);
        add(new Anchor(new StreamResource("Erwachsenen_Training.xlsx", Exporter.exportAsExcel(grid)), "Download als Excel"));
    }
}

// Print
// JavaScript.getCurrent().execute("print();");