Java Servlet - 将响应导出到 Excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16193970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet - export response to Excel file
提问by sree127
I'm unable to export the response from a servlet to an excel file. Please see the code below:
我无法将响应从 servlet 导出到 excel 文件。请看下面的代码:
Test.java:
测试.java:
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
out = response.getWriter();
out.print("<form name=\"test\" method=\"post\" action=\"Export\">");
out.print("<table border=\"1\" cellpadding=\"3\" bordercolor='black'");
out.print("<tr>");
out.print("<td>1</td>");
out.print("<td>hello how are you?</td>");
out.print("</tr>");
out.print("</table>");
out.print("<td><input type=\"submit\" name =\"submit1\" value=\"Export To Excel\"></td>");
out.print("</form>");
The submit button when clicked produce an excel sheet which doesn't contain any values. See the Export.java
which is called when submit button is clicked.
单击提交按钮时会生成一个不包含任何值的 Excel 工作表。查看Export.java
单击提交按钮时调用的 。
Export.java
导出.java
public class Export extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String submit1 = request.getParameter("submit1");
if (submit1 != null) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=users.xls");
}
}
}
Also, it has been observed that if i write the below code in Test.java
, its working fine and the excel sheet does contain the table values.
此外,据观察,如果我在Test.java
.
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=users.xls");
Is there any way to forward the output of Export.java
to Test.java
when the submit button is clicked.
单击提交按钮时,是否有任何方法可以将输出转发Export.java
到Test.java
。
回答by junk
You have to call setContentType before printing the response. You can't print your HTML table and then have the user click a button that takes them to a completely different servlet that does nothing but set the setContentType to Excel and expect that somehow it remembers your HTML table. You also should really look into getting an Excel library for Java (e.g. Apache POI) and using application/msexcel rather than relying on application/vnd.ms-excel to convert HTML to Excel, if this project is meant to have any real-world usefulness.
您必须在打印响应之前调用 setContentType。您不能打印 HTML 表,然后让用户单击一个按钮,将它们带到一个完全不同的 servlet,该 servlet 只将 setContentType 设置为 Excel,并期望它以某种方式记住您的 HTML 表。您还应该真正考虑为 Java 获取 Excel 库(例如 Apache POI)并使用 application/msexcel 而不是依赖 application/vnd.ms-excel 将 HTML 转换为 Excel,如果该项目旨在拥有任何现实世界用处。
回答by sree127
Finally, I found a solution! I stored all the out.print()
stuff into a StringBuffer
. Then using getServletContext().setAttribute("test", Buffer);
I forwarded the whole content into another servlet and from the other servlet I retrieved the data using StringBuffer data = (StringBuffer) getServletContext().getAttribute("test");
.
终于,我找到了解决办法!我把所有的out.print()
东西都存储到一个StringBuffer
. 然后使用getServletContext().setAttribute("test", Buffer);
我将整个内容转发到另一个 servlet 并从另一个 servlet 我使用StringBuffer data = (StringBuffer) getServletContext().getAttribute("test");
.
And finally,
最后,
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=sample.xls");
response.getWriter().write(data.toString());`
did the job.
完成了工作。