我在我的 Bean 中生成了一个 exportList:
public void exportExcel() throws WriteException {
try {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"hours.xls\";");
OutputStream out = response.getOutputStream();
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("de", "DE"));
WritableWorkbook workbook = Workbook.createWorkbook(out, ws);
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
sheet.addCell(new Label(0, 0, "ID", bold));
int row = 1;
for (Hour hour : this.listHours) {
sheet.addCell(new Label(0, row, String.valueOf(hour.getId())));
row++;
}
SheetFormatter.setOptimalColumnWidth(sheet);
workbook.write();
workbook.close();
response.flushBuffer();
context.responseComplete();
context.addMessage(null, new FacesMessage("Liste Exportiert"));
}
catch (Exception e) {
}
}
在我的页面中,我调用 p:commandButton 中的方法
<p:commandButton value="#{msg.export}" update="growl"
immediate="true" action="#{hoursView.exportExcel()}" />
我的页面不会打开 excel 列表...如果添加属性 ajax="false" 它可以工作,但更新将不会执行...
有关信息,如果这会产生一些差异,我的 Bean 是 SessionScoped