简体   繁体   English

如何在Vaadin中出色地导出网格?

[英]How export grid to excel in vaadin?

I use vaadin in my project, and i cant understend how export grid to excel. 我在项目中使用vaadin,但我不能低估导出网格如何出类拔萃。

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 . 有一个关于在vaadin的书文件下载的文件在这里 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 createResosurce方法中,在服务器上写入xlsx文件
  3. Provide StreamResource to the file. 提供StreamResource到文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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