springMvc使用自定义View生成Excel表格

1:通过自定义的View视图可以让请求直接到一个Excel表去。

2:自定义的视图必须继承  AbstractXlsView /AbstractXlsxView / AbstractXlsxStreamingView其中的一个抽象类,并实现buildExcelDocument方法;

3:如果使用的org.springframework.web版本比较老,继承的是AbstractExcelView类,所以说这个类已经过时了。


 

具体实现:

1.使用Maven或者Gradle导包,这里以Gradle为例。

dependencies {
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.4.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '5.0.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '5.0.5.RELEASE'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    compile group: 'javax.servlet', name: 'jsp-api', version: '2.0'
    /*poi生成Excel*/
    compile group: 'org.apache.poi', name: 'poi', version: '4.0.1'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.0.1'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}
View Code

2.创建一个类:

ViewExcel
import com.lwj.pojo.User;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public class ViewExcel extends AbstractXlsxView {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = "账号列表.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/ms-excel");// 文件下载
        response.setHeader("Content-Disposition", "inline; filename=" + new String(fileName.getBytes(), "iso8859-1"));
        OutputStream outputStream = response.getOutputStream();

        Sheet sheet = workbook.createSheet("账号表");

        Row row = sheet.createRow(0);

        //设置标头
        String[] headers = new String[] { "编号", "姓名","密码","年龄" };
        for (int i = 0; i < headers.length; i++) {
            row.createCell(i).setCellValue(headers[i]);
        }

        List<User> list = (List<User>) model.get("userlist");
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);

            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(i + 1);
            row.createCell(1).setCellValue(user.getName());
            row.createCell(2).setCellValue(user.getPassWord());
            row.createCell(3).setCellValue(user.getAge());
        }

        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }
}
View Code

 

3.配置自定义视图解析器

 <!-- 注册excel视图解析器 -->
 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

 <bean id="exportExcel" class="com.lwj.custom.view.ViewExcel"/>

4.控制器

package com.lwj.controller;

import com.lwj.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping("excel")
    public String viewExcel(ModelMap map){
        List<User> list = new ArrayList<>();

        User user = new User();
        user.setName("zs1");
        user.setPassWord("12345");
        user.setAge(27);
        list.add(user);

        user = new User();
        user.setName("zs2");
        user.setPassWord("12346");
        user.setAge(28);
        list.add(user);

        user = new User();
        user.setName("zs3");
        user.setPassWord("12347");
        user.setAge(29);
        list.add(user);

        map.put("userlist", list);
        return "exportExcel";
    }
}
View Code

 

访问控制器的路径就能直接下载Excel表了

posted @ 2018-12-25 20:07  房东的Tom  阅读(935)  评论(0编辑  收藏  举报