Hello, what is wrong in my code: Grid grid_cheque_info = new

Hello, what is wrong in my code:

Grid grid_cheque_info = new Grid<>(ReportCheck.class);
grid_cheque_info.addStyleName(“orderGrid_1”);
grid_cheque_info.setItems(getDataFactory().getListReportCheck());
grid_cheque_info.setWidth(100, Unit.PERCENTAGE);
grid_cheque_info.setSelectionMode(SelectionMode.NONE);

		grid_cheque_info.addColumn(report_check -> {
    		return report_check.getOrderId();
    	}).setId("Order Id"); 

    	grid_cheque_info.addColumn(report_check -> {
    		return report_check.getCheckId(); 
    	}).setId("Check Id");  
    	
    	grid_cheque_info.addColumn(report_check -> {
    		return report_check.getShiftId(); 
    	}).setId("Shift Id");  

and

 	Button downloadAsExcel = new Button("Download As Excel");
	StreamResource excelStreamResource = new StreamResource((StreamResource.StreamSource) () -> Exporter.exportAsExcel(grid_cheque_info), "my-excel.xls");
	FileDownloader excelFileDownloader = new FileDownloader(excelStreamResource);
	
	excelFileDownloader.extend(downloadAsExcel);

when I click on the button, then will be generated excel file, but empty.
I’m using vaadin 8.5.4.
Please help me! Thanks!

Hi Timofey,
This addon works based on the property names. So I think you just need to change the id of the columns. i.e.

grid_cheque_info.addColumn(report_check -> {
    return report_check.getOrderId();
}).setId("orderId"); 

grid_cheque_info.addColumn(report_check -> {
    return report_check.getCheckId(); 
}).setId("checkId");  

grid_cheque_info.addColumn(report_check -> {
    return report_check.getShiftId(); 
}).setId("shiftId");

Hope this helps.

Hi Haijian Wang, thank you for the reply. unfortunately result the same.
This is code of: ReportCheck
public class ReportCheck {

private Integer orderId;
private Integer checkId;
private Integer shiftId;

public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}

public Integer getCheckId() {
	return checkId;
}
public void setCheckId(Integer checkId) {
	this.checkId = checkId;
}

public Integer getShiftId() {
	return shiftId;
}
public void setShiftId(Integer shiftId) {
	this.shiftId = shiftId;
}


public ReportCheck() {;}

public ReportCheck(Integer orderId,
				   Integer checkId,
				   Integer shiftId) {
	this.orderId = orderId;
	this.checkId = checkId;
	this.shiftId = shiftId;
}	

}

Hi Timofey, I just tried your code, below is the whole code and it works. Maybe you can take a look at what’s the difference and restart the server after the code changes to see if it helps.

@Theme("mytheme")
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();

        Grid<ReportCheck> grid_cheque_info = new Grid<>();
        grid_cheque_info.setItems(new ReportCheck(1, 2, 3), new ReportCheck(4, 5, 6));
        grid_cheque_info.addColumn(report_check -> {
            return report_check.getOrderId();
        }).setId("orderId");

        grid_cheque_info.addColumn(report_check -> {
            return report_check.getCheckId();
        }).setId("checkId");

        grid_cheque_info.addColumn(report_check -> {
            return report_check.getShiftId();
        }).setId("shiftId");

        Button downloadAsExcel = new Button("Download As Excel");
        StreamResource excelStreamResource = new StreamResource((StreamResource.StreamSource) () -> Exporter.exportAsExcel(grid_cheque_info), "my-excel.xls");
        FileDownloader excelFileDownloader = new FileDownloader(excelStreamResource);

        excelFileDownloader.extend(downloadAsExcel);

        layout.addComponents(downloadAsExcel, grid_cheque_info);

        setContent(layout);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

Hi Haijian Wang, thank you for your help. I have one more question. What do you think, it can be because I using vaadin 8.5.2 in portlet (Portal Liferay 7)?

Oh, good point. I haven’t really tested with Liferay/Portlet, might be the reason.

It’s works! I’m added to the osgi container two external lib: slf4j-simple-1.7.25.jar and slf4j-api-1.7.25.jar. Thank you.
I have one more question. Can I some manipulate with data, that will be written to the excel? For example I have data:
Order Id 35
Check Id 27
Shift Id 32
Date Of Creation Fri Apr 26 00:00:00 GMT 2019
Date Of Payment -
Cashier Id 20155

  1. I would like to convert date format to yyyy.MM.dd

  2. I would like to rename name of column

  3. How I can generate excel by clicking on the button. For example

     Button unloadToExel = new Button("");
     unloadToExel.addClickListener(new ClickListener() {
     	@Override
     	public void buttonClick(ClickEvent event) {
     		try {
     		String name = "report_check_" + currentDate() +".xls";
     		
     			StreamResource excelStreamResource = new StreamResource((StreamResource.StreamSource) () -> Exporter.exportAsExcel(grid_cheque_info), name);
     			FileDownloader excelFileDownloader = new FileDownloader(excelStreamResource);
     			excelFileDownloader.extend(unloadToExel);
    
     		} catch (Exception e) {
     			Notification.show(e.getMessage(), Notification.Type.ERROR_MESSAGE);
     			e.printStackTrace();
     		}
     	}
     });
    

Is is possible?