在maven中添加poi依赖:
<!-- poi依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.14</version>
</dependency>
编写页面stuList.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function(){
$("#imp").hide();
$("#in").bind("click",function(){
$("#excel").click();
});
$("#excel").bind("change",function(){
$("#submitFormDate").click();
});
$("#out").bind("click",function(){
window.location.href="exportData.action";
});
});
</script>
</head>
<body>
学生信息<br>
<input type="button" id="in" value="导入"/> <input type="button" id="out" value="导出"/>
<table width="60%" border="1px">
<thead>
<tr>
<th>Id</th>
<th>姓名</th>
<th>性别</th>
<th>出生年月</th>
<th>电话</th>
<th>毕业院校</th>
</tr>
</thead>
<tbody>
<c:forEach items="${stuList }" var="stu">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.gender }</td>
<td>${stu.birthday }</td>
<td>${stu.tele }</td>
<td>${stu.collage }</td>
</tr>
</c:forEach>
</tbody>
</table>
<div id="imp">
<form action="importData.action" method="post" enctype="multipart/form-data">
<input type="file" id="excel" name="excelFile"/>
<input type="submit" id="submitFormDate"/>
</form>
</div>
</body>
</html>
编写两个工具类,一个是ExcelBean:
package com.sl.linguan.util;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
//创建一个封装表格数据的bean
public class ExcelBean {
private String headTextName; //表头(标题)名
private String propertyName; //列名
private Integer cols;//单元格数
private XSSFCellStyle cellStyle;
public ExcelBean() {
super();
}
public ExcelBean(String headTextName, String propertyName) {
super();
this.headTextName = headTextName;
this.propertyName = propertyName;
}
public ExcelBean(String headTextName, String propertyName, Integer cols) {
super();
this.headTextName = headTextName;
this.propertyName = propertyName;
this.cols = cols;
}
public String getHeadTextName() {
return headTextName;
}
public void setHeadTextName(String headTextName) {
this.headTextName = headTextName;
}
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public Integer getCols() {
return cols;
}
public void setCols(Integer cols) {
this.cols = cols;
}
public XSSFCellStyle getCellStyle() {
return cellStyle;
}
public void setCellStyle(X