SpringMVC 5.0.4集成easyexcel读取excel文档
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC 5.0.4集成easyexcel读取excel文档相关的知识,希望对你有一定的参考价值。
参考技术A
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。Easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到KB级别,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式。在上层做了模型转换的封装,让使用者更加简单方便。Easyexcel的Github地址:https://github.com/alibaba/easyexcel。
EasyExcel优势
· 注解式自定义操作。
· 输入输出简单,提供输入输出过程的接口
· 支持一定程度的单元格合并等灵活化操作
EasyExcel劣势
· 框架不成熟,1.1.0版本后提供灵活接口的只剩beta版本
· 依然存在一些bug
· 没有一套完整的api
第一步:pom.xml添加依赖,引入jar包:
版本依赖:
第二步:POJO对象编写:
Student.java实体对象继承BaseRowModel,编写如下:
第三步:编写监听器,实现对导入的excel文档的处理,实例如下:
注:具体数据库入库业务代码本文略过,请对应实现业务。
第四步:控制类中调用,进入导入页JSP页addBatchArchivesUser.jsp方法为toAddBatchArchivesUser(String backFlag),保存数据方法为:addBatchArchivesUser(@RequestParam("file") MultipartFile mfile,String orgId,String carCategory)代码如下:
注核心要点:一、根据上传文件后缀为xls、xlsx对ExcelReader中传对应的枚举参数,实现对office 2003 以前的版本和之后版本的区分处理;二、通过监听器的构造函数,从控制类传要调用的服务类(userService);三、通过监听器相关的get方法,将处理结果返回,如本文中的listener.getResult();以上三点,对于初次使用者容易出错。
SpringMVC
文章目录
Spring MVC多动作控制器
- MultiActionController类用于在单个控制器中分别映射多个URL到对应的方法。
package com.yiibai.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("home");
model.addObject("message", "Home");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Add");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Remove");
return model;
}
}
URL映射配置文件
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/home.html" class="com.yiibai.springmvc.UserController" />
<bean name="/user/*.html" class="com.yiibai.springmvc.UserController" />
- 对于
/home.html
请求,DispatcherServlet
将请求转发到UserController
类的home()
方法。 - 对于
user/add.html
,DispatcherServlet
将请求转发到UserController
类的 add()方法。 - 对于
user/remove.html
,DispatcherServlet
将请求转发到UserController
类的remove()
方法。
Spring MVC多动作控制器样例
- 创建一个名称为
MultiActionController
的动态WEB项目 - 在
com.yiibai.springmvc
包下创建一个Java类UserController
。 - 在
jsp
子文件夹下创建两个视图文件:home.jsp 和 user.jsp
UserController.java
package com.yiibai.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("home");
model.addObject("message", "Home");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Add");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Remove");
return model;
}
}
MultiActionController-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/home.html"
class="com.yiibai.springmvc.UserController" />
<bean name="/user/*.html"
class="com.yiibai.springmvc.UserController" />
</beans>
home.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home</title>
</head>
<body>
<body>
<a href="user/add.html" >add()方法</a> <br>
<a href="user/remove.html" >remove()方法</a>
</body>
</html>
user.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User.jsp Page</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
Spring MVC属性方法名称解析器
MultiActionController
类可在单个控制器中分别映射多个URL
到对应的方法
package com.yiibai.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "主页面(Home方法)");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "添加(Add方法)");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "删除(Remove方法)");
return model;
}
}
URL映射配置文件
<bean class="com.yiibai.springmvc.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/user/home.html">home</prop>
<prop key="/user/add.html">add</prop>
<prop key="/user/update.html">update</prop>
</props>
</property>
</bean>
</property>
</bean>
- 对于
/user/home.html
请求,DispatcherServlet
将请求转发到UserController
类的home()
方法 - 对于
user/add.html
,DispatcherServlet
将请求转发到UserController
类的add()
方法。 - 对于
user/remove.html
,DispatcherServlet
将请求转发到UserController
类的remove()
方法
Spring MVC属性方法名称解析器样例
- 创建一个名称为 PropertiesMethodNameResolver 的动态WEB项目。
- 在 com.yiibai.springmvc 包下创建一个Java类UserController。
- 在jsp子文件夹下创建一个视图文件:user.jsp。
UserController.java
package com.yiibai.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "主页面(Home方法)");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "添加(Add方法)");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "删除(Remove方法)");
return model;
}
}
PropertiesMethodNameResolver-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true" />
</bean>
<bean class="com.yiibai.springmvc.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/user/home.html">home</prop>
<prop key="/user/add.html">add</prop>
<prop key="/user/remove.html">remove</prop>
</props>
</property>
</bean>
</property>
</bean>
</beans>
user.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User.jsp Page</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
Spring MVC参数方法名称解析器
MultiActionController
类可在单个控制器中分别映射多个URL到对应的方法
package com.yiibai.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Home");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Add");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Remove");
return model;
}
}
URL映射配置文件
<bean class="com.yiibai.springmvc.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"/>
</bean>
</property>
</bean>
- 对于
/user/*.html?action=home
请求,DispatcherServlet
将请求转发到UserController类的 home()
方法。 - 对于
/user/*.html?action=add
,DispatcherServlet
将请求转发到UserController类的 add()
方法。 - 对于
/user/*.html?action=remove
,DispatcherServlet
将请求转发到UserController类的 remove()
方法
Spring MVC参数方法名称解析器样例
- 创建一个名称为 ParameterMethodNameResolver 的动态WEB项目
- 在 com.yiibai.springmvc 包下创建一个Java类UserController
- 在jsp子文件夹下创建一个视图文件:user.jsp
UserController.java
package com.yiibai.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "主页面(Home)");
return model;
}
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "添加(Add方法)");
return model;
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "删除(Remove方法)");
return model;
}
}
ParameterMethodNameResolver-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true" />
</bean>
<bean class="com.yiibai.springmvc.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"/>
</bean>
</property>
</bean>
</beans>
user.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User.jsp Page</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
Spring MVC可参数化的视图控制器
- 可参数化视图允许将请求映射到网页。
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Home");
return model;
}
}
URL映射配置文件
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
index.html=userController
</value>
</property>
</bean>
<bean id="userController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="index"/>
</bean>
- 对于/index.html请求,DispatcherServlet会将请求转发到UserController控制器,并将viewName设置为index.jsp。
Spring MVC可参数化的视图控制器样例
- 创建一个名称为 ParameterizableViewController 的动态WEB项目
- 在 com.yiibai.springmvc 包下创建一个Java类UserController
- 在jsp子文件夹下创建一个视图文件:index.jsp
UserController.java
package com.yiibai.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController{
public ModelAndView home(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Home");
return m以上是关于SpringMVC 5.0.4集成easyexcel读取excel文档的主要内容,如果未能解决你的问题,请参考以下文章
Excel神办公—使用EasyExce实现数据“有对象写入”和“无对象写入”