一、要把一个html文件给保存成excel,先要一个页面为入口indexServlet.jsp,准备一个html文件excel.html,和一个java类ExcelHello,修改web.xml.
二、具体内容
1、
indexServlet.jsp的内容
<HTML>
<HEAD>
<TITLE> linetuTest </TITLE>
</HEAD>
<BODY>
<a href="hello">hello</a>
<!-- href指定的hello在web.xml中配置 -->
<!-- web.xml指定了java类 -->
<!-- 通过调用java的方法,重定向了一个页面。这样页面提交到类,然后通过类再返回到指定页面就完成了。 -->
<!-- MyJsp2.jsp是一个post提交,本页面属于get提交 -->
</BODY>
</HTML>
2、excel.html
<html>
<head>
<title>西城区残疾人信息管理系统</title>
</head>
<body>
<table width="750" border="1" cellpadding="0" cellspacing="0" id="list_table">
<tr>
<td colspan="20" align="center">各地区残疾类别统计表</td>
</tr>
<tr>
<td rowspan="2" align="center">区域</td>
<td colspan="2" align="center">视力残疾</td>
<td colspan="2" align="center">听力残疾</td>
<td colspan="2" align="center">言语残疾</td>
<td colspan="2" align="center">肢体残疾</td>
<td colspan="2" align="center">智力残疾</td>
<td colspan="2" align="center">精神残疾</td>
<td width="5%" rowspan="2" align="center">合计</td>
<td width="5%" rowspan="2" align="center">男</td>
<td width="5%" rowspan="2" align="center">女</td>
</tr>
<tr>
<td width="5%" height="26" align="center">男</td>
<td width="5%" align="center">女</td>
<td width="5%" height="26" align="center">男</td>
<td width="5%" align="center">女</td>
<td width="5%" height="26" align="center">男</td>
<td width="5%" align="center">女</td>
<td width="5%" height="26" align="center">男</td>
<td width="5%" align="center">女</td>
<td width="5%" height="26" align="center">男</td>
<td width="5%" align="center">女</td>
<td width="5%" height="26" align="center">男</td>
<td width="5%" align="center">女</td>
</tr>
<tr>
<td align="center">西长安街街道办事处</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
<tr>
<td align="center">新街口街道办事处</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
<tr>
<td align="center">什刹海街道办事处</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
<tr>
<td align="center">月坛街道办事处</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
<tr>
<td align="center">展览路街道办事处</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
<tr>
<td align="center">德胜街道办事处</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
<tr>
<td align="center">金融街街道办事处</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
<tr>
<td align="center">合计</td>
<td width="5%" height="26">
</td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
<td width="5%" height="26"> </td>
<td width="4%"> </td>
<!-- <td width="5%"> </td>
<td width="5%"> </td>-->
<td width="4%"> </td>
<td width="4%"> </td>
<td width="4%"> </td>
</tr>
</table>
</body>
</html>
3、ExcelHello
package common.util;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ExcelHello extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
doGet(req,resp);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
/*PrintWriter out = resp.getWriter();
out.println("Hello World!");
out.close();*/
// 导出EXCEL
resp.setContentType ( "application/ms-excel" );//设置页面ContentType的属性
resp.setHeader("Content-disposition","attachment; filename=book.xls" );//将页面导出成Excel文件
//resp.sendRedirect("excel.html");//重新定向到一个页面
getServletContext().getRequestDispatcher("/excel.html").forward(req, resp);
}
}
4、web.xml 在该文件中添加
<servlet>
<servlet-name>SimpleHello</servlet-name>
<servlet-class>common.util.ExcelHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleHello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
5.MyJsp2.jsp
<html>
<head>
<title>To Excel starting page</title>
</head>
<body>
This is my JSP page. <br>
<form action="hello" method="post">
<input type="text" name="user">
<input type="submit" value="toExcel">
</form>
</body>
</html>