在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

基于Spring Mvc实现的Excel文件上传下载的实例代码

2017-3-7 12:50| 发布者: zhangjf| 查看: 905| 评论: 0

摘要: 最近工作遇到一个需求,需要下载excel模板,编辑后上传解析存储到数据库。因此为了更好的理解公司框架,我就自己先用spring mvc实现了一个样例。 基础框架 之前曾经介绍过一个最简单的spring mvc的项目如何搭建,传 ...

最近工作遇到一个需求,需要下载excel模板,编辑后上传解析存储到数据库。因此为了更好的理解公司框架,我就自己先用spring mvc实现了一个样例。

基础框架

之前曾经介绍过一个最简单的spring mvc的项目如何搭建,传送门在这里。

这次就基于这个工程,继续实现上传下载的小例子。需要做下面的事情:

1 增加index.html,添加form提交文件

2 引入commons-fileupload、commons-io、jxl等工具包

3 创建upload download接口

4 注入multipartResolver bean

5 在upload中使用HttpServletRequest获取文件流,通过WorkBook进行解析

6 在download中通过HttpServerResponse返回文件流,实现下载

页面

页面很简单,其实就是一个form标签,需要注意的是:

  • form中enctype="multipart/form-data"
  • action指定访问的url
  • input中需要设置name属性,这样后端才能获取到文件对象
  1. <form role="form" action="/upload" method="POST" enctype="multipart/form-data">
  2. <div class="form-group">
  3. <label for="file">上传文件</label>
  4. <input type="file" id="file" name="file">
  5. </div>
  6. <button type="submit" class="btn btn-default">提交</button>
  7. </form>
复制代码

引入commons-fileupload、jxl等工具包

涉及的jar包有:

  • commons-fileupload 用于获取上传文件
  • jxl 用于解析excel
  1. <!-- springframework begins -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-webmvc</artifactId>
  5. <version>4.2.4.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-context</artifactId>
  10. <version>4.2.4.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>javax.servlet</groupId>
  14. <artifactId>javax.servlet-api</artifactId>
  15. <version>4.0.0-b01</version>
  16. </dependency>
  17. <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
  18. <dependency>
  19. <groupId>commons-io</groupId>
  20. <artifactId>commons-io</artifactId>
  21. <version>2.5</version>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
  24. <dependency>
  25. <groupId>commons-fileupload</groupId>
  26. <artifactId>commons-fileupload</artifactId>
  27. <version>1.3.2</version>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/jexcelapi/jxl -->
  30. <dependency>
  31. <groupId>jexcelapi</groupId>
  32. <artifactId>jxl</artifactId>
  33. <version>2.6</version>
  34. </dependency>
复制代码

Xml的配置

在web.xml中需要配置默认的访问页面,因为之前已经设置过拦截的请求是/,因此如果不设置所有的静态页面都会被拦截下来。

  1. <welcome-file-list>
  2. <welcome-file>index.html</welcome-file>
  3. </welcome-file-list>
复制代码

在spring的配置文件中,加入CommonsMultipartResolver的bean。

  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  2. <!-- set the max upload size100MB -->
  3. <property name="maxUploadSize">
  4. <value>104857600</value>
  5. </property>
  6. <property name="maxInMemorySize">
  7. <value>4096</value>
  8. </property>
  9. </bean>
复制代码

上传代码

  1. @RequestMapping("upload")
  2. public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException, BiffException, WriteException {
  3. MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
  4. MultipartFile file = mRequest.getFile("file");
  5. Workbook workbook = Workbook.getWorkbook(file.getInputStream());
  6. //遍历Sheet页
  7. Arrays.stream(workbook.getSheets())
  8. .forEach(sheet -> {
  9. int size = sheet.getRows();
  10. for(int i=0; i<size; i++){
  11. //遍历每一行,读取每列信息
  12. Arrays.stream(sheet.getRow(i)).forEach(cell -> System.out.println(cell.getContents().equals("")?'空':cell.getContents()));
  13. }
  14. });
  15. response.setHeader("Content-Disposition", "attachment; filename=return.xls");
  16. WritableWorkbook writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream());
  17. writableWorkbook.write();
  18. writableWorkbook.close();
  19. }
复制代码

下载代码

  1. @RequestMapping("download")
  2. public void download(HttpServletRequest request, HttpServletResponse response) throws IOException, BiffException, WriteException {
  3. response.setHeader("Content-Disposition", "attachment; filename=template.xls");
  4. WritableWorkbook writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream());
  5. writableWorkbook.write();
  6. writableWorkbook.close();
  7. }
复制代码

模板类

  1. static class ExcelUtils {
  2. public static WritableWorkbook createTemplate(OutputStream output) throws IOException, WriteException {
  3. WritableWorkbook writableWorkbook= Workbook.createWorkbook(output);
  4. WritableSheet wsheet = writableWorkbook.createSheet("测试title", 0);
  5. CellFormat cf = writableWorkbook.getSheet(0).getCell(1, 0).getCellFormat();
  6. WritableCellFormat wc = new WritableCellFormat();
  7. // 设置居中
  8. wc.setAlignment(Alignment.CENTRE);
  9. // 设置边框线
  10. // wc.setBorder(Border.ALL, BorderLineStyle.THIN);
  11. wc.setBackground(jxl.format.Colour.GREEN);
  12. Label nc0 = new Label(0, 0, "标题1",wc);//Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是z
  13. Label nc1 = new Label(1, 0, "标题2",wc);
  14. Label nc2 = new Label(2, 0, "标题3",wc);
  15. Label nc3 = new Label(0, 1, "dddd");
  16. Label nc4 = new Label(1, 1, "ffff");
  17. wsheet.addCell(nc0);
  18. wsheet.addCell(nc1);
  19. wsheet.addCell(nc2);
  20. wsheet.addCell(nc3);
  21. wsheet.addCell(nc4);
  22. return writableWorkbook;
  23. }
  24. }
复制代码

最后贡献下相关的代码

有需要的可以拿去参考

网盘下载链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 02:41

Copyright 2015-2025 djqfx

返回顶部