Hello Wang, I am getting empty excel and empty csv content inside while c

Hello Wang,

I am getting empty excel and empty csv content inside while clicking the anchor Links in my application in vaadin 13.using version 3.0.0.
code:
grid.addColumn(counterNameSummary → counterNameSummary.getPageRequests() != null ? counterNameSummary.getPageRequests().toString() : “”).setHeader(“Pagerequests”).setFlexGrow(0).setWidth(“120px”).setSortable(true).setKey(“Pagerequests”);

add(new Anchor(new StreamResource(“my-excel.xls”, Exporter.exportAsExcel(grid)), “Download As Excel”));
add(new Anchor(new StreamResource(“my-excel.csv”, Exporter.exportAsCSV(grid)), “Download As CSV”));
grid.getDataProvider().refreshAll();

Hi Siva

The column key should be in camel case, try setKey("pageRequests")

Hello Wang,
Thank you for your reply.

Still it shows Downloader popup with 0 bytes for CSV and excel.How to resolve the issue?

Code:

 private Grid<CounterNameSummary> grid = new Grid<CounterNameSummary>(CounterNameSummary.class); 

grid.addColumn(counterNameSummary -> counterNameSummary.getCounterName().getCountryCode().getCountryCode()).setHeader("Language").setFlexGrow(0).setWidth("95px").setKey("language");
grid.addColumn(counterNameSummary -> counterNameSummary.getPageVisitors() != null ? counterNameSummary.getPageVisitors().toString() : "").setHeader("Visitors").setFlexGrow(0).setWidth("95px").setKey("visitors");
grid.addColumn(counterNameSummary -> counterNameSummary.getPageRequests() != null ? counterNameSummary.getPageRequests().toString() : "").setHeader("Pagerequests").setFlexGrow(0).setWidth("120px").setSortable(true).setKey("pageRequests");
        
add(new Anchor(new StreamResource("my-excel.xls", Exporter.exportAsExcel(grid)), "Download As Excel"));	        
add(new Anchor(new StreamResource("my-excel.csv", Exporter.exportAsCSV(grid)), "Download As CSV"));

 grid.getDataProvider().refreshAll();

Hi Siva

I didn’t see grid.setItems() or grid.setDataProviders() in the code above. Did you set data to the grid?

A couple of other things:

  1. For the Exporter addon to work, a column key should match a getter method in the POJO, so for the language column, you need to have a getLanguage() method in CounterNameSummary.
  2. No need to call grid.getDataProvider().refreshAll(); unless you have changed some items in the data provider

Hello Wang,
Thank you for your reply. Now i am able produce the excel file and csv files.
How to handle when pagination is available in the grid.?Because now it download only visible items in the screen list.

if (result != null && result.size() != 0) {
			grid.getColumns().forEach(grid::removeColumn);
			grid.setItems(result.size() >= pageSize ? result.subList(0, pageSize - 1) : result);
			lastFetchNumber = result.size();
			grid.addColumn(counterNameSummary -> counterNameSummary.getCounterName().getCountryCode().getCountryCode())
					.setHeader("Language").setFlexGrow(0).setWidth("95px")
					.setKey("counterName.countryCode.countryCode");
		}
		add(new Anchor(new StreamResource("my-excel.xls", Exporter.exportAsExcel(grid)), "Download As Excel"));
		add(new Anchor(new StreamResource("my-excel.csv", Exporter.exportAsCSV(grid)), "Download As CSV"));

Thank you.

Hi Siva

Currently this addon doesn’t support paging. It supports lazy dataprvoder though, if that’s an option.

Just out of curiosity, how the paging was implemented, did you use some addon? or you made your own implementation.