基于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文件上传下载功能模块的实现
----------------------------------------------------------------------------------------------[版权申明: ...
随机推荐
- Nginx %00空字节执行php漏洞
Nginx如下版本:0.5.*, 0.6.*, 0.7 <= 0.7.65, 0.8 <= 0.8.37在使用PHP-FastCGI执行php的时候,URL里面在遇到%00空字节时与Fas ...
- 增删改查--windows下mysql客户端--表的使用
>>>>>>>>>>>>>>>>>>>> selet 5种子句之where常用运 ...
- Android下的屏幕适配
1080 100dp 300px720 100dp 200px 300px 1080px 比例是300/1080=0.277200px 720px
- 多线程之互斥锁(By C++)
首先贴一段win32API实现的多线程的代码,使用CreateThread实现,如果不要传参数,就把第四个参数设为NULL #include<Windows.h> #include< ...
- dedecms 文章页图片改为绝对路径
这几天在网站改版,想把网站做大,想做频道页二级域名,于是在做网站的过程中发现一个问题,dedecms开设二级域名后,在二级域名的文章页无法显示图片,查看源代码后发现问题,由于dedecms文章页中的图 ...
- jQuery之ajax错误调试分析
jQuery中把ajax封装得非常好.但是日常开发中,我偶尔还是会遇到ajax报错.这里简单分析一下ajax报错 一般的jQuery用法如下,ajax通过post方式提交"汤姆和老鼠&quo ...
- C++: Perfect Forwarding
Link: Rvalue References and Perfect Forwarding in C++0x (https://www.justsoftwaresolutions.co.uk/cpl ...
- CSharp数据库代码生成工具
项目中遇到很多数据库表字段特别多的项目,手动一个一个去敲也不知道敲到什么时候,突发奇想做一个工具一劳永逸.花了一晚上做了这个东西,代码写的比较乱,用这个工具后减少了很多时间. Git地址:https: ...
- BeautifulSoup
参考:http://www.freebuf.com/news/special/96763.html 相关资料:http://www.jb51.net/article/65287.htm 1.Pytho ...
- 文档:网络通讯包结构(crc校验,加解密)
一直想把这个流程整理一下. 包结构: 包 对(datacrc+protoID+dataSize)组成的byte[] 进行crc计算而得到 对(数据内容)进行crc计算而得到 协议号 数据内容的字节长度 ...