SSM框架使用poi导入导出Excel的详细方法

2025-05-29 0 90

1.首先我们先导入poi和文件上传的依赖

  1. <!–POI–>
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi-ooxml-schemas</artifactId>
  5. <version>3.14-beta1</version>
  6. </dependency>
  7. <!–文件上传依赖–>
  8. <dependency>
  9. <groupId>commons-fileupload</groupId>
  10. <artifactId>commons-fileupload</artifactId>
  11. <version>1.2.2</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>commons-io</groupId>
  15. <artifactId>commons-io</artifactId>
  16. <version>2.4</version>
  17. </dependency>

2.在spring-mvc.xml中配置文件上传解析器

  1. <!– 配置文件上传解析器 –>
  2. <!– id 的值是固定的–>
  3. <bean id="multipartResolver"
  4. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  5. <!– 设置上传文件的最大尺寸为 5MB –>
  6. <property name="maxUploadSize">
  7. <value>5242880</value>
  8. </property>
  9. </bean>

3.创建index.html

  1. <!– excel文件导出 –>
  2. <p><a href="User/exportExcel.do" rel="external nofollow" >导出</a>
  3. <!– excel文件导入 –>
  4. <form action="User/importExcel.do" method="post" enctype="multipart/form-data">
  5. <input type="file" name="userExcel" />
  6. <input type="submit" value="导入">
  7. </form>

4.创建实体类

  1. public class User {
  2. private Integer id;
  3. private String username;
  4. private String password;
  5. /* get 和 set */
  6. }

5.Controller层

  1. /**
  2. * 导出Excel
  3. * @param request
  4. * @param response
  5. */
  6. @RequestMapping("/exportExcel")
  7. @ResponseBody
  8. public void exportExcel(HttpServletRequest request, HttpServletResponse response){
  9. try {
  10. //获取数据源
  11. List<User> userList = service.queryUserAll();
  12. //导出excel
  13. response.setHeader("Content-Disposition","attachment;filename="+new String("用户信息.xls".getBytes(),"ISO-8859-1"));
  14. response.setContentType("application/x-excel;charset=UTF-8");
  15. OutputStream outputStream = response.getOutputStream();
  16. //导出
  17. service.exportExcel(userList,outputStream);
  18. outputStream.close();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  1. /**
  2. * 导入exc
  3. * @param userExcel
  4. * @param request
  5. * @param session
  6. * @return
  7. */
  8. @RequestMapping("/importExcel")
  9. @ResponseBody
  10. public String importExcel(MultipartFile userExcel, HttpServletRequest request, HttpSession session) throws IOException, InvalidFormatException {
  11. if(userExcel == null){
  12. session.setAttribute("excelName", "未上传文件,上传失败!");
  13. return null;
  14. }
  15. String userExcelFileName = userExcel.getOriginalFilename();
  16. if(!userExcelFileName.matches("^.+\\\\.(?i)((xls)|(xlsx))$")){
  17. session.setAttribute("excelName", "文件格式不正确!请使用.xls或.xlsx后缀的文档,导入失败!");
  18. return null;
  19. }
  20. //导入
  21. service.importExcel(userExcel);
  22. session.setAttribute("excelName", "导入成功!");
  23. return "redirect:queryUserAll.do";
  24. }

6.运行测试

SSM框架使用poi导入导出Excel的详细方法

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

SSM框架使用poi导入导出Excel的详细方法

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

SSM框架使用poi导入导出Excel的详细方法

SSM框架使用poi导入导出Excel的详细方法

到此这篇关于SSM框架使用poi导入导出Excel的文章就介绍到这了,更多相关SSM框架导入导出Excel内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/weixin_51311866/article/details/115246070

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SSM框架使用poi导入导出Excel的详细方法 https://www.kuaiidc.com/107813.html

相关文章

发表评论
暂无评论