easypoi-excel导入案例

1.依赖

  <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>

2.实体类

package com.yl.jasperreportdemo.domain;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;

import java.io.Serializable;
import java.util.Date;

@ExcelTarget("student")
public class Student implements Serializable {
    @Excel(name = "name")
    private String name;
    @Excel(name = "age")
    private int age;
    @Excel(name = "sex",replace = { "男_0", "女_1",})
    private int sex;
    @Excel(name = "date",format = "yyyy-MM-dd")
    private Date birthDay;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthDay=" + birthDay +
                '}';
    }
}

3.准备好要导入的excel数据,路径为F:/test1.xlsx

在这里插入图片描述

4.测试

package com.yl.jasperreportdemo.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.yl.jasperreportdemo.domain.Student;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.List;

public class ImportController {
    public static void main(String[] args) {
        ImportParams params = new ImportParams();
        //表格标题行数,默认0
        params.setTitleRows(0);
        //表头行数,默认1
        params.setHeadRows(1);
        List<Student> list = ExcelImportUtil.importExcel(
                new File("F:/test1.xlsx"),
                Student.class, params);
        System.out.println(list.size());
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(item -> {
                System.out.println(item);
            });
        }
    }
}

5.结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值