java - 通过 Glassfish Server 在 Java Servlet 中构建 Excel 表格

标签 java servlets jakarta-ee netbeans glassfish

我提到了 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

enter image description here

这是程序,

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 服务器一起使用。同时我什至无法构建 PDF。我是否缺少插件或其他东西?

最佳答案

只有当浏览器能够在浏览器窗口内打开文件时,您想要的行为才有可能。浏览器需要安装 MS Excel 和一个插件才能在浏览器窗口中打开 MS Excel(这类似于在浏览器中运行 flash - 在安装 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'");

关于java - 通过 Glassfish Server 在 Java Servlet 中构建 Excel 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34117584/

相关文章:

java - 与 spring 的 Servlet 映射

java - 单击电子邮件激活链接时如何登录用户?(JSF 2.0)

java - 按顺序打印线程并为每个线程设置随机 sleep 时间

java - 如何在 java 中传递类型列表而不是接口(interface)

java - 使用 JSoup 合并 Java 中的 HTML 文件

javascript - Java servlet 对 JavaScript 的响应

java - 使用 Stripe 时 Maven 编译错误 : Cannot find symbol APIException, APIConnectionException

java - 无法在 Eclipse Google App Engine 项目中添加 servlet

servlets - 必须声明元素 web-app(Servlet 4.0)

java.lang.ClassNotFoundException : org. postgresql.Driver问题