easyexcel 上传下载

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

easyexcel 上传下载

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

示例


pojo 层

Person

@Data
public class Person {

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

config 层

WebConfig

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
    }
}

CustomListener

public class CustomListener extends AnalysisEventListener<Person> {

    @Override
    public void invoke(Person data, AnalysisContext context) {
        System.out.println(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("数据解析完成");
    }
}

controller 层

HelloController

@RestController
public class HelloController {

    @RequestMapping("/upload")
    public Map<String,Object> upload(MultipartFile file){
                                                   //参数的名称设置为file
        
        Map<String,Object> result=new HashMap<>();

        try {
            EasyExcel.read(file.getInputStream(), Person.class,new CustomListener()).sheet().doRead();

            result.put("code",0);
            result.put("msg","上传成功");
        }catch (Exception e){
            result.put("code",1);
            result.put("msg",e.getMessage());
        }

        return result;
    }

    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws Exception{
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        String fileName= URLEncoder.encode("测试数据", StandardCharsets.UTF_8);
        response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");

        EasyExcel.write(response.getOutputStream(),Person.class).sheet().doWrite(data());
    }

    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;
    }
}

前端页面

                     

index.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<script>
    layui.use(["upload","layer"],function () {
        const upload = layui.upload;
        const layer = layui.layer;

        upload.render({
            elem: "#btn",
            url: "/upload",
            accept: "file",
            done: function (result) {
                layer.msg(result.msg,{icon:6})
            }
        })
    })
</script>

<body>
<div th:align="center">
    <button id="btn" class="layui-btn">
        <i class="layui-icon layui-icon-upload"></i>上传文件
    </button>
</div>
</body>
</html>

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

使用测试


上传文件

localhost:8080/index

                     

上传文件

                     

点击上传文件,文件上传后,控制台输出

Person(id=0, name=瓜田李下0, age=20)
Person(id=1, name=瓜田李下1, age=21)
Person(id=2, name=瓜田李下2, age=22)
Person(id=3, name=瓜田李下3, age=23)
Person(id=4, name=瓜田李下4, age=24)
Person(id=5, name=瓜田李下5, age=25)
数据解析完成

下载文件

localhost:8080/download

                     

                     

相关文章