简体   繁体   中英

How export grid to excel in vaadin?

I use vaadin in my project, and i cant understend how export grid to excel.

I have:

Grid<Vlr> gridVlr = new Grid<>("Vlrs"); 
gridBank.setItems(VlrList); // i used ArrayList
gridBank.addColumn(Vlr::getVlrId).setCaption("VlrId");

ExportExcelComponentConfiguration<Vlr> componentConfig1
    = new ExportExcelComponentConfigurationBuilder<Vlr>()
        .withGrid(gridVlr)
        .withVisibleProperties(new String[]{"Vlr ID"})
        .withHeaderConfigs(Arrays.asList(
            new ComponentHeaderConfigurationBuilder()
            .withAutoFilter(true)
            .withColumnKeys(new String[]{"Vlr Id"})
            .build()
        ))
        .withIntegerFormattingProperties(Arrays.asList("counter"))
        .withFloatFormattingProperties(Arrays.asList(
            "totalCosts",
            "differenceToMin"
        ))
        .withBooleanFormattingProperties(Arrays.asList("active"))
        //  .withColumnFormatters(columnFormatters)
        .build();

ExportExcelSheetConfiguration<Vlr> sheetConfig1
    = new ExportExcelSheetConfigurationBuilder<Vlr>()
        .withReportTitle("Grid (Default)")
        .withSheetName("Grid (default)")
        .withComponentConfigs(Arrays.asList(componentConfig1))
        .withIsHeaderSectionRequired(Boolean.TRUE)
        .withDateFormat("dd-MMM-yyyy")
        .build();


ExportExcelConfiguration<Vlr> config
    = new ExportExcelConfigurationBuilder<Vlr>()
        .withGeneratedBy("Roman")
        .withSheetConfigs(Arrays.asList(sheetConfig1))
        .build();

ExportToExcel exportToExcel = new ExportToExcel(ExportType.XLSX, null);

Button buttonExport = new Button("Export to Excel", clickEvent -> {
    exportToExcel.export();
});

layout.addComponent(buttonExport);

What's my mistake? Maybe there is a simpler solution?

There is documentation about file download in book of vaadin here . You are missing download "link" from your button.

Steps to download a file from button click are following

  1. Create button with download resource
  2. In createResosurce method write xlsx file on the server
  3. Provide StreamResource to the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM