A simple tool for exporting data from Grid to Excel or CSV. Exporter 1.x.x for Vaadin 7, Exporter 2.x.x for Vaadin 8, Exporter 3.x.x for Vaadin 10+
Exporter.exportAsExcel(grid));
Exporter.exportAsCSV(grid))
A typical use case is to let user download the generated file, an Anchor component can be used for such a purpose.
Vaadin 10+
new Anchor(new StreamResource("my-excel.xlsx", Exporter.exportAsExcel(grid)), "Download As Excel");
Vaadin 8
Button downloadAsExcel = new Button("Download As Excel");
StreamResource excelStreamResource = new StreamResource((StreamResource.StreamSource) () -> Exporter.exportAsExcel(grid), "my-excel.xlsx");
FileDownloader excelFileDownloader = new FileDownloader(excelStreamResource);
excelFileDownloader.extend(downloadAsExcel);
Use instructions
Each to be exported column should have a key, and the key should be a property name. A key will be generated automatically if you are also passing the class in the Grid constructor parameter, e.g.
Grid<Person> grid = new Grid<>(Person.class);
Otherwise, you need to set the key manually. e.g.
Grid<Person> grid = new Grid<>();
grid.addColumn(Person::getEmail).setKey("email");
Exporter works as using reflection to read property from each item, a column without a valid key will be ignored.
Exporter 1 for Vaadin 7 works a bit differently. It's a Vaadin Button to download the exported files and it's based on Vaadin Container. Code Example on how to use Exporter 1
ExcelExporter excelExporter = new ExcelExporter();
excelExporter.setDateFormat("yyyy-MM-dd");
excelExporter.setTableToBeExported(sampleTable);
excelExporter.setCaption("Export to Excel");
layout.addComponent(excelExporter);
Install
Added .xlsx support, also makes .xlsx the default file when calling Export.exportAsExcel(). To export as .xls file, call Export.exportAsXls()