使用JSF导出Excel时出现问题

发布于 2024-07-26 04:53:31 字数 2435 浏览 2 评论 0原文

我正在使用 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

I'm using JSF/ICEFaces. The page is ICEFaces however I'm using the JSF datatable because ICEFaces had slow performance for some reason. Anyway, unlike the ICEFaces datatable, the JSF table doesn't come with export to excel so I'm writing my own. I decided to use Apache POI as below. The code executes well but I don't see pop up to save excel file. Am I missing something?

    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();
    }

And the excel button is:

            <h:commandButton id="excelBtn"
               rendered="#{statsDisplayAndFilter.renderNextBtn}"
                image="./xmlhttp/css/rime/css-images/excel.png"
                actionListener="#{statsDisplayAndFilter.ExportWithPoi}"/>

Thanks,

Tam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2024-08-02 04:53:31

我建议您将此代码放入其自己的 servlet 中。 您正在尝试缩短 JSF 的通常生命周期,虽然您可能能够找到一种方法来做到这一点,但简单地创建一个处理此 XLS 文件的新 servlet 并让它执行可能会更简单,甚至可能更干净它自己的事情。 您可以通过操作按钮将适当的对象临时放入会话中,将它们定向到 servlet,在 servlet 中抓取它们,然后将它们从会话中删除。

I'd suggest you pull this code into its own servlet. You're trying to short circuit JSF's usual life-cycle and while you might be able to find a way to do it, it would probably be simpler and possibly even cleaner to simply make a new servlet which handles this XLS file and let it do its own thing. You can place the appropriate objects into session temporarily from your action button, direct them to the servlet, grab them in the servlet and then remove them from session.

高冷爸爸 2024-08-02 04:53:31

我专门针对 ICEFaces 实现发布了一篇博客文章此处

I have posted a blog post specifically for an ICEFaces implementation here.

画▽骨i 2024-08-02 04:53:31

使用按钮操作而不是 Actionlistener

对我有用

Use button action instead of Actionlistener

Worked for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文