其他分享
首页 > 其他分享> > EasyExcel导入简单使用

EasyExcel导入简单使用

作者:互联网

实体类

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;

import java.util.Date;

@ExcelIgnoreUnannotated//忽略未注解(如果需要忽略的项比较少可以在忽略项上使用@ExcelIgnore)
public class Student{
    @ExcelProperty("姓名")
    private String name;

    @ExcelProperty("年龄")
    @NumberFormat
    private Integer age;

    @ExcelProperty("性别")
    private String gender;

    @ExcelProperty("爱好")
    private String hobby;

    @ExcelProperty("出生日期")
    @DateTimeFormat("yyyy/MM/dd")
    @ColumnWidth(20)
    private Date dateOfBirth;

//    @ExcelIgnore//忽略项(如果需要忽略的项比较多可以在类上使用@ExcelIgnoreUnannotated)
    private String irrelevant;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getGender() {
        return gender;
    }

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

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getIrrelevant() {
        return irrelevant;
    }

    public void setIrrelevant(String irrelevant) {
        this.irrelevant = irrelevant;
    }

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

测试类

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.example.demo.model.Student;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/easyExcel")
public class EasyExcelController {

    /**
     * @Title: importExcel
     * @Description: 导入
     * @Param: [file]
     * @return: void
     **/
    @RequestMapping("/importExcel")
    public String importExcel(@RequestParam("file") MultipartFile file) throws Exception{

        ExcelReaderBuilder excelReaderBuilder = EasyExcelFactory.read(file.getInputStream(),Student.class,null);
        List<Student> data = excelReaderBuilder.doReadAllSync();
        if(data != null && data.size() != 0){
            for(Student student : data){
                System.out.println(student);
            }
        }
        return "index";
    }
  
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
    <h1>这是主页</h1>
    <div>
        <form action="http://localhost:8081/easyExcel/importExcel" method="post" enctype="multipart/form-data">
            <input type="file" name="file"/>
            <input type="submit" value="导入" />
        </form>

    </div>
    <div>
        <a href="/easyExcel/exportExcel">导出EasyExcel</a>
    </div>
</body>
</html>

 

 

 

 

标签:return,String,EasyExcel,annotation,导入,简单,import,com,public
来源: https://www.cnblogs.com/wdk2020/p/16277520.html