springboot excel 视图(poi)

x33g5p2x  于2021-12-28 转载在 其他  
字(2.7k)|赞(0)|评价(0)|浏览(1026)

springboot excel 视图(poi)

应用:使用视图将数据导出,保存到excel文件中

说明:可直接使用easyexcel下载数据,保存到excel文件

*******************

相关类与接口

AbstractXlsView

public abstract class AbstractXlsView extends AbstractView {
    public AbstractXlsView() {
        this.setContentType("application/vnd.ms-excel");
    }

    protected boolean generatesDownloadContent() {
        return true;
    }

    protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Workbook workbook = this.createWorkbook(model, request);
        this.buildExcelDocument(model, workbook, request, response);
        response.setContentType(this.getContentType());
        this.renderWorkbook(workbook, response);
    }

    protected Workbook createWorkbook(Map<String, Object> model, HttpServletRequest request) {
        return new HSSFWorkbook();
    }

    protected void renderWorkbook(Workbook workbook, HttpServletResponse response) throws IOException {
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        workbook.close();
    }

    protected abstract void buildExcelDocument(Map<String, Object> var1, Workbook var2, HttpServletRequest var3, HttpServletResponse var4) throws Exception;
}

*******************

示例


pojo 层

Person

@Data
public class Person {

    private Integer id;
    private String name;
    private Integer age;
}

controller 层

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(ModelAndView mv){
        mv.addObject("list",data());
        mv.setView(initView("测试数据"));

        return mv;
    }

    @SuppressWarnings("unchecked")
    private View initView(String fileName){
        return new AbstractXlsView() {

            @Override
            protected void buildExcelDocument(Map<String, Object> map, Workbook workbook, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
                if (fileName !=null){
                    httpServletResponse.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(fileName, StandardCharsets.UTF_8)+".xlsx");
                }

                List<Person> list=(List<Person>)(map.get("list"));

                Sheet sheet=workbook.createSheet();
                Row head=sheet.createRow(0);
                head.createCell(0).setCellValue("id");
                head.createCell(1).setCellValue("name");
                head.createCell(2).setCellValue("age");

                for (int i=0;i<list.size();i++){
                    Person person=list.get(i);

                    Row row=sheet.createRow(i+1);
                    row.createCell(0).setCellValue(person.getId());
                    row.createCell(1).setCellValue(person.getName());
                    row.createCell(2).setCellValue(person.getAge());
                }
            }
        };
    }

    private List<Person> data(){
        List<Person> list=new ArrayList<>();

        for (int i=0;i<5;i++){
            Person person=new Person();
            person.setId(i);
            person.setName("瓜田李下 "+i);
            person.setAge(20+i);

            list.add(person);
        }

        return list;
    }
}

*******************

使用测试

localhost:8080/hello

                       

                      

相关文章