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

网友投稿 459 2023-06-08


基于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属性,这样后端才能获取到文件对象

引入commons-fileupload、jxl等工具包

涉及的jar包有:

commons-fileupload 用于获取上传文件

jxl 用于解析excel

org.springframework

spring-webmvc

4.2.4.RELEASE

org.springframework

spring-context

4.2.4.RELEASE

javax.servlet

javax.servlet-api

4.0.0-b01

commons-io

commons-io

2.5

commons-fileupload

commons-fileupload

1.3.2

jexcelapi

jxl

2.6

Xml的配置

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

index.html

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

104857600

4096

上传代码

@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

//遍历每一行,读取每列信息

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.ahttp://ddCell(nc4);

return writableWorkbook;

}

}

最后贡献下相关的代码:SpringTest_jb51.rar

//遍历每一行,读取每列信息

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.ahttp://ddCell(nc4);

return writableWorkbook;

}

}

最后贡献下相关的代码:SpringTest_jb51.rar


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Android 判断真机和模拟器的方法
下一篇:详解Spring mvc的web.xml配置说明
相关文章

 发表评论

暂时没有评论,来抢沙发吧~