SSM框架使用poi导入导出Excel的详细方法
更新时间:2021年03月29日 09:56:48 作者:杨延超
这篇文章主要介绍了SSM框架使用poi导入导出Excel,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
1.首先我们先导入poi和文件上传的依赖
<!--POI-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14-beta1</version>
</dependency>
<!--文件上传依赖-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2.在spring-mvc.xml中配置文件上传解析器
<!-- 配置文件上传解析器 -->
<!-- id 的值是固定的-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为 5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
3.创建index.html
<!-- excel文件导出 --> <p><a href="User/exportExcel.do" rel="external nofollow" >导出</a> <!-- excel文件导入 --> <form action="User/importExcel.do" method="post" enctype="multipart/form-data"> <input type="file" name="userExcel" /> <input type="submit" value="导入"> </form>
4.创建实体类
public class User {
private Integer id;
private String username;
private String password;
/* get 和 set */
}
5.Controller层
/**
* 导出Excel
* @param request
* @param response
*/
@RequestMapping("/exportExcel")
@ResponseBody
public void exportExcel(HttpServletRequest request, HttpServletResponse response){
try {
//获取数据源
List<User> userList = service.queryUserAll();
//导出excel
response.setHeader("Content-Disposition","attachment;filename="+new String("用户信息.xls".getBytes(),"ISO-8859-1"));
response.setContentType("application/x-excel;charset=UTF-8");
OutputStream outputStream = response.getOutputStream();
//导出
service.exportExcel(userList,outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 导入exc
* @param userExcel
* @param request
* @param session
* @return
*/
@RequestMapping("/importExcel")
@ResponseBody
public String importExcel(MultipartFile userExcel, HttpServletRequest request, HttpSession session) throws IOException, InvalidFormatException {
if(userExcel == null){
session.setAttribute("excelName", "未上传文件,上传失败!");
return null;
}
String userExcelFileName = userExcel.getOriginalFilename();
if(!userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
session.setAttribute("excelName", "文件格式不正确!请使用.xls或.xlsx后缀的文档,导入失败!");
return null;
}
//导入
service.importExcel(userExcel);
session.setAttribute("excelName", "导入成功!");
return "redirect:queryUserAll.do";
}
6.运行测试

1.点击导出将数据库的内容以后缀为 .xls的文件下载下来

2. 选择Excel文件点击导入会将文件里的内容导入到数据库中


到此这篇关于SSM框架使用poi导入导出Excel的文章就介绍到这了,更多相关SSM框架导入导出Excel内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringMVC中RequestMapping注解(作用、出现的位置、属性)
这篇文章主要介绍了SpringMVC中RequestMapping注解(作用、出现的位置、属性),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01
SpringBoot+thymeleaf+ajax实现局部刷新详情
这篇文章主要介绍了SpringBoot+thymeleaf+ajax实现局部刷新详情,文章围绕主题展开详细的内容介绍具有一定的参考价值,需要的小伙伴可以参考一下2022-09-09
SpringBoot使用Redisson实现延迟执行的完整示例
这篇文章主要介绍了SpringBoot使用Redisson实现延迟执行的完整示例,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下2024-06-06
Mybatis利用分页插件PageHelper快速实现分页查询
如果你也在用MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件,这篇文章主要给大家介绍了关于Mybatis利用分页插件PageHelper快速实现分页查询的相关资料,PageHelper是一个Mybatis的分页插件,负责将已经写好的sql语句,进行分页加工,需要的朋友可以参考下2021-08-08


最新评论