在后台管理中,有时候系统管理员需要将后台数据进行批量导入到数据库中,在这里我们介绍一种方法。注:在本案例中导入的数据量并不大,只是用来做微小体量的数据导入,所以没有考虑大体量情况下,导入速度优化的问题。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SSM框架Excel文件操作</title>
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jQuery.print.min.js"></script>
<style type="text/css">
</style>
</head>
<body>
<div id="head" align="center">
<table>
<tr>
<td><input type="file" id="upload" name="upload" value="" /></td>
<td><button onclick="uploadFile()">上传</button></td>
</tr>
</table>
</div>
<!-- JavaScript代码如下 -->
<script type="text/javascript">
function uploadFile() {
var file = $("#upload").val();
file = file.substring(file.lastIndexOf('.'), file.length);
if (file == '') {
alert("上传文件不能为空!");
} else if (file != '.xlsx' && file != '.xls') {
alert("请选择正确的excel类型文件!");
} else {
ajaxFileUpload();
}
}
function ajaxFileUpload() {
var formData = new FormData();
var name = $("#upload").val();
formData.append("file", $("#upload")[0].files[0]);
formData.append("name", name);
$.ajax({
url : "InputExcel.do",
type : "POST",
async : false,
data : formData,
processData : false,
contentType : false,
beforeSend : function() {
console.log("正在进行,请稍候");
},
success : function(e) {
if (e == "01") {
alert("导入成功");
} else {
alert("导入失败");
}
}
});
}
</script>
</body>
</html>
# student.java
private String studentExamId;
private String studentId;
private String studentName;
private String profession;
private String studyStyle;
private String queryTime;
private String ipAddress;
private String studentIDcard;
private String studentGrade;
private String admitTime;
private String enterTime;
private String telephone;
## StudentMapper.java
//从Excel中将学生信息导入到数据库中
void InputExcel(Map<String, Object> ginsengMap);
## StudentMapper.xml(mybaties)
<!--从Excel中将学生信息导入到数据库中 -->
<insert id="InputExcel">
insert into student (studentExamId,studentId,studentName,profession,studyStyle,queryTime,ipAddress,studentIDcard,studentGrade,
admitTime,enterTime,telephone) values
(#{studentExamId },#{studentId },#{studentName },#{profession },#{studyStyle},#{queryTime},#{ipAddress},#{studentIDcard},#{studentGrade},#{admitTime},#{enterTime},#{telephone})
</insert>
## studentService.java
//从Excel中导入学生信息到数据库中
public String InputExcel(InputStream is, String originalFilename) {
Map<String,Object> ginsengMap = new HashMap<String,Object>();
List<ArrayList<Object>> list;
if (originalFilename.endsWith(".xls")) {
list = Excel.readExcel2003(is);
} else {
list = Excel.readExcel2007(is);
}
for (int i=0,j=list.size();i<j;i++){
List<Object> row = list.get(i);
ginsengMap.put("studentExamId", row.get(0).toString());
ginsengMap.put("studentId", row.get(1).toString());
ginsengMap.put("studentName", row.get(2).toString());
ginsengMap.put("profession", row.get(3).toString());
ginsengMap.put("studyStyle", row.get(4).toString());
ginsengMap.put("queryTime", row.get(5).toString());
ginsengMap.put("ipAddress", row.get(6).toString());
ginsengMap.put("studentIDcard", row.get(7).toString());
ginsengMap.put("studentGrade", row.get(8).toString());
ginsengMap.put("admitTime", row.get(9).toString());
ginsengMap.put("enterTime", row.get(10).toString());
ginsengMap.put("telephone", row.get(11).toString());
studentMapper.InputExcel(ginsengMap);
}
return "01";
}
## StudentController.java
//实现将Excel表格中的学生信息导入到数据库中
@RequestMapping(value = "/InputExcel.do")
@ResponseBody
public String InputExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
String flag = "02";// 上传标志
if (!file.isEmpty()) {
try {
String originalFilename = file.getOriginalFilename();// 原文件名字
log.info("文件名:" + originalFilename);
InputStream is = file.getInputStream();// 获取输入流
flag = studentService.InputExcel(is, originalFilename);
} catch (Exception e) {
flag = "03";// 上传出错
e.printStackTrace();
}
}
return flag;
}
在项目建好以后,在Excel中有能会报以下错误:
1.The method getCell(short) in the type HSSFRow is not applicable for the arguments (int)
2. The method isCellDateFormatted(HSSFCell) in the type HSSFDateUtil is not applicable for the arguments (XSSFCell)
导致的原因及处理方法*
由于poi-3.0.2-FINAL-20080204.jar包和poi-3.7-20101029.jar包两个版本冲突,这个问题也是当时困扰了我将近一天。只保留poi-3.7-20101029.jar,其余的jar包都要删除,并且在buildPath中去除已经创建加载的类库。
**
下载的链接地址(包括一个已经打包的完整例子):