美文网首页
解析Excel数据

解析Excel数据

作者: 自由主义者 | 来源:发表于2019-10-25 17:15 被阅读0次
    first:导入依赖
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>easyexcel</artifactId>
          <version>1.1.2-beta4</version>
      </dependency>
    
    second:读取
        @Transactional
        public Map<String, Object> batchQuitExcel(MultipartFile file, String userType) throws Exception {
            if (file.isEmpty()) {
                throw new Exception("请上传文件");
            } else {
                InputStream inputStream = file.getInputStream();
                List<Object> list = EasyExcelFactory.read(inputStream, new Sheet(1, 1, User.class));
                inputStream.close();
            }
       }    
    

    User实体类

    import java.io.Serializable;
    import java.util.Date;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Transient;
    import org.springframework.format.annotation.DateTimeFormat;
    import com.alibaba.excel.annotation.ExcelProperty;
    import com.alibaba.excel.metadata.BaseRowModel;
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import lombok.Data;
    import tk.mybatis.mapper.annotation.KeySql;
    
    /**
     * 用户信息
     *
     */
    @Entity
    @Table(name = "t_user")
    @Data
    public class User extends BaseRowModel implements Serializable {
    
        @Id
        @Column(length = 32, nullable = false)
        @KeySql(genId = UUIDGenId.class)
        private String id;
    
        private static final long serialVersionUID = -8822374049581195909L;
    
        //工号
        @ExcelProperty(index = 0)
        private String jobNumber;
    
        //用户昵称
        @ExcelProperty(index = 1)
        private String userName;
    
        //出生日期
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @ExcelProperty(index = 5, format = "yyyy-MM-dd")
        private Date birthday;
    
        //邮箱
        @ExcelProperty(index = 3)
        private String email;
    
        //性别(1、男,2、女)
    
        private Integer gender;
    
        //从excel导入用户,接收男 女
        @Transient
        @ExcelProperty(index = 2)
        private String genderStr;
    
        //入职日期
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @ExcelProperty(index = 6, format = "yyyy-MM-dd")
        private Date hiredate;
    
        //身份证号
        @ExcelProperty(index = 7)
        private String identity;
    
        //密码密文
        private String password;
    
        //备注
        @ExcelProperty(index = 8)
        private String remarks;
    
        //手机号
        @ExcelProperty(index = 4)
        private String userPhone;
    
        //有效期
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date validTime;
    
    }
    

    相关文章

      网友评论

          本文标题:解析Excel数据

          本文链接:https://www.haomeiwen.com/subject/rwtevctx.html