SpringMVC下实现Excel文件上传下载

网友投稿 292 2023-02-11


SpringMVC下实现Excel文件上传下载

在实际应用中,经常会遇到上传Excel或者下载Excel的情况,比如导入数据、下载统计数据等等场景。针对这个问题,我写了个基于SpringMVC的简单上传下载示例,其中ExgxfTWMjdbcel的处理使用Apache的POI组件。

主要依赖的包如下:

commons-io

commons-io

2.4

commons-fileupload

commons-fileupload

1.3.1

org.springframework

spring-web

4.0.0.RELEASE

org.springframework

spring-webmvc

4.0.0.RELEASE

org.apache.poi

poi

3.10.1

相关处理类:

(一)Controller类

package com.research.spring.controller;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.ModelAndView;

import com.research.spring.model.UserInfo;

import com.research.spring.view.ExcelView;

@Controller

@RequestMapping("/file")

public class FileController {

/**

* Excel文件上传处理

* @param file

* @return

*/

@RequestMapping("/upload")

public ModelAndView uploadExcel(@RequestParam("file") MultipartFile file){

List list = new ArrayList();

//这里只处理文件名包括“用户”的文件,模板使用下载模板

if( file.getOriginalFilename().contains("用户") ){

try {

Workbook wb = new HSSFWorkbook(file.getInputStream());

Sheet sheet = wb.getSheetAt(0);

for( int i = 1; i <= sheet.getLastRowNum(); i++ ){

Row row = sheet.getRow(i);

UserInfo info = new UserInfo();

info.setUserName(row.getCell(0).getStringCellValue());

info.setPassword(row.getCell(1).getStringCellValue());

list.add(info);

}

} catch (IOException e) {

e.printStackTrace();

}

}

ModelAndView mav = new ModelAndView("content");

mav.addObject("content",list.toString());

return mav;

}

/**

* Excel文件下载处理

*/

@RequestMapping("/download")

public ModelAndView downloanExcel(){

List list = new ArrayList();

UserInfo userInfo = new UserInfo();

userInfo.setPassword("0000");

userInfo.setUserName("sdfas");

list.add(userInfo);

list.add(userInfo);

list.add(userInfo);

list.add(userInfo);

Map> map = new HashMap>();

map.put("infoList", list);

ExcelView ve = new ExcelView();

return new ModelAndView(ve,map);

}

}

(二)实体类

package com.research.spring.model;

public class UserInfo {

private String userName;

private String password;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Override

public String toString() {

return "UserInfo [userName=" + userName + ", password=" + password

+ "]";

}

}

(三)View类

这个类在下载时用到,在Spring渲染页面时使用自定义的View类进行Excel的相关处理。

package com.research.spring.view;

import java.io.OutputStream;

import java.net.URLEncoder;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.research.spring.model.UserInfo;

/**

* 下载Excel视图

*

* @author wdmcygah

*

*/

public class ExcelView extends AbstractExcelView {

@Override

protectgxfTWMjdbed void buildExcelDocument(Map model,

HSSFWorkbook workbook, HttpServletRequest request,

HttpServletResponse response) throws Exception {

@SuppressWarnings("unchecked")

List list = (List) model.get("infoList");

if (list != null && list.size() != 0) {

int len = list.size();

Sheet sheet = workbook.createSheet();

// 第一行文字说明

Row row = sheet.createRow(0);

Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING);

cell.setCellValue("用户名");

cell = row.createCell(1, Cell.CELL_TYPE_STRING);

cell.setCellValue("密码");

//下面是具体内容

for (int i = 0; i < len; i++) {

row = sheet.createRow(i + 1);

cell = row.createCell(0, Cell.CELL_TYPE_STRING);

cell.setCellValue(list.get(i).getUserName());

cell = row.createCell(1, Cell.CELL_TYPE_STRING);

cell.setCellValue(list.get(i).getPassword());

}

}

response.setContentType("application/vnd.ms-excel");

response.setCharacterEncoding("utf-8");

//这里对文件名进行编码,保证下载时汉字显http://示正常

String fileName = URLEncoder.encode("用户.xls", "utf-8");

//Content-disposition属性设置成以附件方式进行下载

response.setHeader("Content-disposition", "attachment;filename="

+ fileName);

OutputStream os = response.getOutputStream();

workbook.write(os);

os.flush();

os.close();

}

}

(四)主要配置文件

上传文件时需要在配置文件中配置MultipartResolver类,配置后Spring会自动将文件传成MultipartFile对象,然后就可以进行相应的处理。示例看Controller类。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd">

(五)测试页面

如果想看完整源码,可以到我的github仓库查看。 其中,上传文件只处理符合下载模板的文件。若要处理其它文件需要自实现。代码测试通过无误。


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

上一篇:剖析Angular Component的源码示例
下一篇:文件上传接口测试(上传文件的接口怎么测试)
相关文章

 发表评论

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