使用开源的POI组件,包含实现对Excal文件的创建和写入操作的类,使POI组件操作EXCEL文件的步骤
- 创建Excel的工作表,POI组件的HSSFWorkbook类提供了创建工作表的方法 public HSSFSheet
createSheet(String sheetname) //参数sheetname表示工作表的名称 - 创建表格的行,在保存之前创建行对象,该对象由工作表对象创建 public HSSFRow createRow(int
rownum) //参数rownum表示工作表中行对象的行号 - 创建表格的单元格,由HSSFRow类的createRow()方法创建 public HSSFCell createCell(int
columnIndex) //参数columnIndex表示单元格对象的列编号 - 写入单元格内容,常用的是String类型的字符串数据 public void setCellValue(String value)
index.jsp页面
<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
<title>用户注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
table{
font-size:12px;
font-family: 隶书;
color:gray;
border: 1px green solid;
}
input{
font-size:12px;
font-family: 隶书;
color:gray;
}
</style>
</head>
<body>
<form action="export" method="post">
<table align="center">
<tr>
<td>用户名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="sex" value="男" />男
<input type="radio" name="sex" value="女" />女
</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="导出到Excel" />
</td>
</tr>
</table>
</form>
</body>
</html>
ExportServlet的Servlet类,doPost方法获得信息,POI组件将信息导出到POI组件
public class ExportServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");//设置响应正文的MIME类型,该类型表示Excel
//response.addHeader("Content-Disposition", "attachment;filename=logininfo.xls");
String name = request.getParameter("name");
String pwd =request.getParameter("pwd");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String email = request.getParameter("email");
ServletOutputStream out = response.getOutputStream();//响应输出流对象
HSSFWorkbook wb = new HSSFWorkbook(); //创建Excel表格
HSSFSheet sheet = wb.createSheet("用户注册信息"); //创建工作簿
sheet.setColumnWidth(4, 5000); //设置列宽
HSSFRow titleRow = sheet.createRow(0); //创建Excel中的标题行
HSSFCell titleCell1 =titleRow .createCell(0); //在行中创建第1个单元格
titleCell1.setCellValue("用户姓名"); //设置第1个单元格的值
HSSFCell titleCell2= titleRow.createCell(1); //在行中创建第2个单元格
titleCell2.setCellValue("密码"); //设置第2个单元格的值
HSSFCell titleCell3 =titleRow .createCell(2); //在行中创建第3个单元格
titleCell3.setCellValue("性别"); //设置第3个单元格的值
HSSFCell titleCell4= titleRow.createCell(3); //在行中创建第4个单元格
titleCell4.setCellValue("年龄"); //设置第4个单元格的值
HSSFCell titleCell5= titleRow.createCell(4); //在行中创建第5个单元格
titleCell5.setCellValue("Email"); //设置第5个单元格的值
HSSFRow valueRow = sheet.createRow(1); //创建第2行
HSSFCell nameCell = valueRow.createCell(0); //在第2行中创建单元格
nameCell.setCellValue(name);
HSSFCell pwdCell = valueRow.createCell(1);
pwdCell.setCellValue(pwd);
HSSFCell sexCell = valueRow.createCell(2);
sexCell.setCellValue(sex);
HSSFCell ageCell = valueRow.createCell(3);
ageCell.setCellValue(age);
HSSFCell emailCell = valueRow.createCell(4);
emailCell.setCellValue(email);
HSSFCellStyle cellStyle = wb.createCellStyle();
wb.write(out); //将响应流输入到Excel表格中
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml文件配置
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ExportServlet</servlet-name>
<servlet-class>com.lh.servlet.ExportServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExportServlet</servlet-name>
<url-pattern>/export</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>