EasyExcel 入门上传下载

EasyExcel 入门

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.1</version>
        </dependency>

下载/到出成excel

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    @ExcelProperty("ID")
    private Integer id;
    @ExcelProperty("姓名")
    private String name;
    @ExcelProperty("余额")
    private double money;
}
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
//好像是固定写法
        response.reset();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition",
                "attachment;filename=user_excel_" + System.currentTimeMillis() + "./xlsx" );

        userService.download(response.getOutputStream());
    }
    public void download(ServletOutputStream outputStream) {
        EasyExcelFactory.write(outputStream,User.class).sheet("User-sheet").doWrite(this::getUserList);
    }

    private Collection<?> getUserList() {
        // 数据穿入
        List<User> list = Collections.singletonList(User.builder().id(1).money(1.1).name("lf").build());
        list.forEach(System.out::println);

        return list;
    }

image-20220830163237682image-20220830163251260

上传

    @PostMapping("/uploading")
    public String uploading(@RequestParam(value = "file",required = true)MultipartFile file) throws IOException {
        userService.upload(file.getInputStream());
        return "ok";
    }
    private List<User> listUser = new ArrayList<>();

    public void upload(InputStream inputStream) {
        EasyExcelFactory.read(inputStream,User.class,new ReadListener<User>(){
            @Override
            public void invoke(User user, AnalysisContext analysisContext) {
                listUser.add(user);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                listUser.forEach(user -> log.info(user.toString()));
            }
        }).sheet().doRead();
    }

image-20220830164649155
image-20220830164659988

底部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值