Я использую JSF / ICEFaces. Страница ICEFaces, однако я использую датированную JSF, потому что ICEFaces по какой-то причине имели низкую производительность. В любом случае, в отличие от таблицы данных ICEFaces, таблица JSF не поставляется с возможностью экспорта в Excel, поэтому я пишу свою собственную. Я решил использовать Apache POI, как показано ниже. Код выполняется хорошо, но я не вижу всплывающее окно, чтобы сохранить файл Excel. Я что-то упустил?
public void ExportWithPoi(ActionEvent e) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
// ArrayList<PerfStatBean> statFilterResults;
Iterator<PerfStatBean> statsIterator = statFilterResults.iterator();
int i=0;
HSSFRow row;
row = sheet.createRow((short)0);
row.createCell((short)0).setCellValue("Current Application ID");
row.createCell((short)1).setCellValue("Event Name");
row.createCell((short)2).setCellValue("Generic Method Name");
while(statsIterator.hasNext()){
i++;
row = sheet.createRow((short)i);
PerfStatBean perfBean = statsIterator.next();
row.createCell((short)0).setCellValue(perfBean.getCurrent_appl_id());
row.createCell((short)1).setCellValue(perfBean.getCurrent_appl_id());
row.createCell((short)2).setCellValue(perfBean.getGeneric_method_name());
}
HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
res.setContentType("application/vnd.ms-excel");
res.setHeader("Content-disposition", "attachment; filename=PerfCollector.xls");
try {
ServletOutputStream out = res.getOutputStream();
wb.write(out);
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
FacesContext faces = FacesContext.getCurrentInstance();
faces.responseComplete();
}
И кнопка Excel:
<h:commandButton id="excelBtn"
rendered="#{statsDisplayAndFilter.renderNextBtn}"
image="./xmlhttp/css/rime/css-images/excel.png"
actionListener="#{statsDisplayAndFilter.ExportWithPoi}"/>
Спасибо
Tam