通过 Glassfish Server 在 Java Servlet 中构建 Excel sheet

Build Excel sheet in Java Servlet through Glassfish Server

我提到了 Coreservlets

My question is when i run following program, it starts to download(which shows nothing inside it) rather than displaying as Excel sheet on the browser as what book shows.No Exceptions happened

这是程序,

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
        response.setContentType("application/vnd.ms-excel");
        PrintWriter out= response.getWriter();
        out.println("\tQ1 \tQ2 \tQ3 \tQ4 \tQ5");
        out.println("Apples \t1 \t2 \t3 \t4 \t5");
        out.println("Oranges \t1 \t2 \t3 \t4 \t5");       
    }

我将 Netbeans IDE 与 Netbeans 附带的 Glassfish 4.1 服务器一起使用 itself.Same 我什至无法构建 PDF too.Am 我缺少插件或其他东西?

您是否像书中那样使用 Internet Explorer Windows 并安装了 Microsoft Office?此示例可能不适用于所有浏览器(Chrome、Mozilla 等)。

如果浏览器能够在浏览器内打开文件,您想要的行为才有可能实现 window。浏览器需要安装 MS Excel 和在浏览器 window 中打开 MS Excel 的插件(这类似于浏览器中的 运行 flash - 在安装之前它不会工作闪存插件)。我不知道这样的插件是否适用于 chrome 或 firefox,因为 Microsoft 的应用程序对其他应用程序不友好。

如果一切就绪,您将很有可能获得理想的行为。如果不是,您可能需要将 Content-Disposition: inline 添加到响应中的 HTTP header 中,如下所述:How to force files to open in browser instead of download (pdf)?

像这样:

response.addHeader("Content-Disposition", "inline; filename='apples_oranges.xls'");