基于Spring Mvc实现的Excel文件上传下载
最近工作遇到一个需求,需要下载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文件上传下载的更多相关文章
- RPC基于http协议通过netty支持文件上传下载
本人在中间件研发组(主要开发RPC),近期遇到一个需求:RPC基于http协议通过netty支持文件上传下载 经过一系列的资料查找学习,终于实现了该功能 通过netty实现文件上传下载,主要在编解码时 ...
- [SAP ABAP开发技术总结]客户端文本文件、Excel文件上传下载
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- ABAP EXCEL 文件上传下载 用SMW0
T-CODE: SMW0 在这里只介绍二进制数据,HTML模板的上传也一样. 另外也可以用CBO TABLE管理文件 可以看我另一个博文:CBO TABLE管理文件上传下载 选择 二进制 写包名: 进 ...
- spring mvc 3.0 实现文件上传功能
http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...
- 基于spring 3.0mvc 框架的文件上传实现
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框 ...
- Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制
拦截器 文件上传 -中文乱码解决 rest风格 异常处理机制 拦截器 Spring MVC可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerI ...
- Spring MVC(四)文件上传
文件上传步骤 1.写一个文件上传的页面 2.写一个文件上传的控制器 注意: 1.method="post" 2.enctype="multipart/form-data& ...
- spring mvc框架+ ajax实现 文件上传
1.前端页面,通过form表单提交,必须设置 enctype="multipart/form-data" 代表form表单在发送到服务器时候编码方式是二进制类型,一般用于图片.mp ...
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
----------------------------------------------------------------------------------------------[版权申明: ...
随机推荐
- js数组操作大全
原文(http://www.cnblogs.com/webhotel/archive/2010/12/21/1912732.html) 用 js有很久了,但都没有深究过js的数组形式.偶尔用用也就是简 ...
- hive学习笔记
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- getComputedStyle 方法
一:getComputedStyle getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值.返回的是一个CSS样式声明对象([object CSSStyleDeclarat ...
- 写给Git初学者的7个建议
[原文] - http://blog.jobbole.com/50603/ 当我刚刚开始使用Git的版本控制时,我根本不确定我付出那么多时间是不是会得到回报.Branch.Stage.Stash,这些 ...
- 用户名 不在 sudoers文件中
切换到root用户,然后加上某个账户 pzdn@CentOs$ su #输入root密码 root@CentOs cd /etc/ chmod 770 sudoers vim sudoers # 找到 ...
- 【转】Oracle索引失效问题
转自:http://www.cnblogs.com/millen/archive/2010/01/18/1650423.html 失效情况分析: <> 单独的>,<,(有时会用 ...
- bzoj1412最小割
太羞耻了,m n写反了(主要是样例n m相等) 建图方法比较高(ji)端(chu),对于可以加栅栏的地方连上1的边,然后求最小割即可 为了让代码优(suo)美(duan),我写了一个check,避免多 ...
- vs2013 手动生成webservice代理类wsdl
第一步: 第二步: 第三步: 至此wsdl代理类生成成功!
- 解决FF浏览器无法执行window.close()脚本
在FF浏览器中输入about:config 查找dom.allow_scripts_to_close_windows 将值改为true
- 在线Spirte图定位工具,自动生成CSS
发现一个在线雪碧图中的图片地位的工具,并且能够自动生成css.spritecow 废话不多说,有图有真相: