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)?
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
I would like to convert date format to yyyy.MM.dd
I would like to rename name of column
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();
}
}
});