最近工作遇到一个需求,需要下载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属性,这样后端才能获取到文件对象
<form role="form" action="/upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="file">上传文件</label>
<input type="file" id="file" name="file">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>

引入commons-fileupload、jxl等工具包

涉及的jar包有:

  • commons-fileupload 用于获取上传文件
  • jxl 用于解析excel
<!-- springframework begins -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jexcelapi/jxl -->
<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency>

Xml的配置

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

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

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

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>

上传代码

@RequestMapping("upload")
public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException, BiffException, WriteException {
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
MultipartFile file = mRequest.getFile("file");
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
//遍历Sheet页
Arrays.stream(workbook.getSheets())
.forEach(sheet -> {
int size = sheet.getRows();
for(int i=0; i<size; i++){
//遍历每一行,读取每列信息
Arrays.stream(sheet.getRow(i)).forEach(cell -> System.out.println(cell.getContents().equals("")?'空':cell.getContents()));
}
}); response.setHeader("Content-Disposition", "attachment; filename=return.xls");
WritableWorkbook writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream());
writableWorkbook.write();
writableWorkbook.close();
}

下载代码

@RequestMapping("download")
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException, BiffException, WriteException {
response.setHeader("Content-Disposition", "attachment; filename=template.xls");
WritableWorkbook writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream());
writableWorkbook.write();
writableWorkbook.close();
}

模板类

static class ExcelUtils {
public static WritableWorkbook createTemplate(OutputStream output) throws IOException, WriteException {
WritableWorkbook writableWorkbook= Workbook.createWorkbook(output);
WritableSheet wsheet = writableWorkbook.createSheet("测试title", 0); CellFormat cf = writableWorkbook.getSheet(0).getCell(1, 0).getCellFormat();
WritableCellFormat wc = new WritableCellFormat();
// 设置居中
wc.setAlignment(Alignment.CENTRE);
// 设置边框线
// wc.setBorder(Border.ALL, BorderLineStyle.THIN);
wc.setBackground(jxl.format.Colour.GREEN); Label nc0 = new Label(0, 0, "标题1",wc);//Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是z
Label nc1 = new Label(1, 0, "标题2",wc);
Label nc2 = new Label(2, 0, "标题3",wc);
Label nc3 = new Label(0, 1, "dddd");
Label nc4 = new Label(1, 1, "ffff"); wsheet.addCell(nc0);
wsheet.addCell(nc1);
wsheet.addCell(nc2);
wsheet.addCell(nc3);
wsheet.addCell(nc4); return writableWorkbook;
}
}

最后贡献下相关的代码

有需要的可以拿去参考

网盘下载链接

基于Spring Mvc实现的Excel文件上传下载的更多相关文章

  1. RPC基于http协议通过netty支持文件上传下载

    本人在中间件研发组(主要开发RPC),近期遇到一个需求:RPC基于http协议通过netty支持文件上传下载 经过一系列的资料查找学习,终于实现了该功能 通过netty实现文件上传下载,主要在编解码时 ...

  2. [SAP ABAP开发技术总结]客户端文本文件、Excel文件上传下载

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. ABAP EXCEL 文件上传下载 用SMW0

    T-CODE: SMW0 在这里只介绍二进制数据,HTML模板的上传也一样. 另外也可以用CBO TABLE管理文件 可以看我另一个博文:CBO TABLE管理文件上传下载 选择 二进制 写包名: 进 ...

  4. spring mvc 3.0 实现文件上传功能

    http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...

  5. 基于spring 3.0mvc 框架的文件上传实现

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框 ...

  6. Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

    拦截器 文件上传 -中文乱码解决 rest风格 异常处理机制 拦截器 Spring MVC可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerI ...

  7. Spring MVC(四)文件上传

    文件上传步骤 1.写一个文件上传的页面 2.写一个文件上传的控制器 注意: 1.method="post" 2.enctype="multipart/form-data& ...

  8. spring mvc框架+ ajax实现 文件上传

    1.前端页面,通过form表单提交,必须设置 enctype="multipart/form-data" 代表form表单在发送到服务器时候编码方式是二进制类型,一般用于图片.mp ...

  9. 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现

    ----------------------------------------------------------------------------------------------[版权申明: ...

随机推荐

  1. Mybatis拦截器

    Mybatis拦截器

  2. 一些VS2013的使用技巧

    作者:h46incon的Blog 1. Peek View 可以在不新建TAB的情况下快速查看.编辑一个函数的代码. 用法:在光标移至某个函数下,按下alt+F12. 然后在Peek窗口里可以继续按a ...

  3. Servlet读取Excel标准化数据库过程记录

    完成数据库的连接 获取连接参数 拷贝1.数据库URL 2.驱动程序类 3.用户 编写Servlet 1.创建连接对象 Connection con = null; PreparedStatement ...

  4. iOS 删除、重新排序xcdatamodel

    找到Xcode项目文件.xcodeproj,查看包内容. 里面有project.pbxproj,用文本编辑器打开. 找到类似如下内容段: /* Begin XCVersionGroup section ...

  5. 关于handler 和 looper 的问题

    重新去学习回顾looper和handler ,还是需要重新认识这个经常使用的机制. 我首先是看任玉刚老师的书<android的开发艺术探索>的第十章. 里面一句话开始说出了我们大概的理解— ...

  6. storm学习好文链接

    大圆的那些事:http://www.cnblogs.com/panfeng412/tag/Storm/ xcc的博客:http://blog.csdn.net/damacheng/article/ca ...

  7. xcode8 升级后注释快键键不能使用的解决方法

    1.这个是因为苹果解决xcode ghost.把插件屏蔽了.解决方法 命令运行:  sudo /usr/libexec/xpccachectl 然后必须重启电脑后生效 2.option+command ...

  8. winfrom组件圆角

    精简后,就其实一点,只要有paint事件的组件,都可画圆角,没有的外面套一个panel就行了. using System; using System.Collections.Generic; usin ...

  9. Ubuntu Java 环境变量

    方法1:修改/etc/profile 文件所有用户的 shell都有权使用这些环境变量<1>在 shell终端执行命令:vi /etc/profile<2>在 profile文 ...

  10. C# Listview 数据绑定

    今天搞Winform,有串数据需要绑定到TabControl里面,原来用datatable,组长说这玩意会有问题不让用,菜鸟实在不会,百度查的Listview用法,写了个数组进去绑定 using Sy ...