我无法将响应从 servlet 导出到 excel 文件。请看下面的代码:
测试.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>");
单击提交按钮时会生成一个不包含任何值的 Excel 表。查看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");
}
}
}
此外,据观察,如果我在 中编写以下代码Test.java
,它的工作正常并且 excel 表确实包含表格值。
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=users.xls");
单击提交按钮时,有什么方法可以将输出转发Export.java
到。Test.java