【JAVA ——EasyExcel的简单使用】

【JAVA ——EasyExcel的简单使用】_第1张图片

 【JAVA ——EasyExcel的简单使用】_第2张图片

 下载jar包

改为1.8

【JAVA ——EasyExcel的简单使用】_第3张图片

 删除不需要的包  org

【JAVA ——EasyExcel的简单使用】_第4张图片

添加资源目录

【JAVA ——EasyExcel的简单使用】_第5张图片 

 添加依赖


  com.alibaba
  easyexcel
  2.2.3

写操作 

test下创建包

【JAVA ——EasyExcel的简单使用】_第6张图片

 

创建代码

package com.dto;

import com.alibaba.excel.annotation.ExcelProperty;

import java.util.Date;

public class User {
    @ExcelProperty("用户编号")
    private Integer userId;
    @ExcelProperty("用户姓名")
    private String userName;
    @ExcelProperty("性别")
    private String gender;
    @ExcelProperty("工资")
    private Double salary;
    @ExcelProperty("入职时间")
    private Date hireDate;

    public User() {
    }

    public User(Integer userId, String userName, String gender, Double salary, Date hireDate) {
        this.userId = userId;
        this.userName = userName;
        this.gender = gender;
        this.salary = salary;
        this.hireDate = hireDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", gender='" + gender + '\'' +
                ", salary=" + salary +
                ", hireDate=" + hireDate +
                '}';
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public Date getHireDate() {
        return hireDate;
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }
}

【JAVA ——EasyExcel的简单使用】_第7张图片


 

 简单写操作01

文件名  模板的class   表名   写哪一个list

    @Test
    public void test01(){
        String fileName = "user1.xlsx";
        List userList =new ArrayList<>();
        User user01=new User(1,"李磊","男",1000.90,new Date());
        User user02=new User(2,"张三","男",2000.90,new Date());
        User user03=new User(3,"李四","男",3000.90,new Date());
        User user04=new User(4,"王五","男",7000.90,new Date());
        userList.add(user01);
        userList.add(user02);
        userList.add(user03);
        userList.add(user04);
        EasyExcel.write(fileName,User.class).sheet("用户信息").doWrite(userList);
    }

 

简单写操作02

构建excelWrite对象

【JAVA ——EasyExcel的简单使用】_第8张图片

不关闭会撑爆的内存


简单写操作:不要几个属性 

1、实体类使用

@ExcelIgnore注解忽视。

2、使用排除方法.excludeColumnFiledNames(set)

        Set set =new HashSet<>();
        set.add("hireDate");
        set.add("salary");
        EasyExcel.write(fileName,User.class)
                .sheet("用户信息3")
                .excludeColumnFiledNames(set)
                .doWrite(userList);


简单写操作:要几个属性  

<

你可能感兴趣的:(java,servlet,开发语言)