1.添加依赖
<!--文件上传-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
2.添加 WDWUtil.java 工具类
用于判断 Excel 版本
3.创建 Read.java 文件
用于读取 Excel,实体类根据实际情况自己替换
4.创建服务层
数据库层根据实际情况编写
5.创建控制层
6.前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String importMsg="";
if(request.getSession().getAttribute("msg")!=null){
importMsg=request.getSession().getAttribute("msg").toString();
}
request.getSession().setAttribute("msg", "");
%>
<head>
<title>批量导入</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<body>
<div><font color="bule">批量导入</font></div>
<form action="addController/batchimport" method="post" enctype="multipart/form-data" onsubmit="return check();">
<div style="margin: 30px;"><input id="excel_file" type="file" name="filename" accept="xlsx" size="80"/>
<input id="excel_button" type="submit" value="导入Excel"/></div>
<font id="importMsg" color="red"><%=importMsg%></font><input type="hidden"/>
</form>
</body>
<script type="text/javascript">
function check() {
var excel_file = $("#excel_file").val();
if (excel_file == "" || excel_file.length == 0) {
alert("请选择文件路径!");
return false;
} else {
return true;
}
}
$(document).ready(function () {
var msg="";
if($("#importMsg").text()!=null){
msg=$("#importMsg").text();
}
if(msg!=""){
alert(msg);
}
});
</script>
</html>
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于