Excel封装注解导入导出

这篇博客主要介绍了如何使用Java来实现Excel的自定义导入和导出功能,通过注解的方式提高开发效率和代码的可读性。内容包括测试文件的准备以及自定义导入和导出注解的详细步骤。

1、测试文件

package com.cmkj.plug.excel.pojo;

import com.cmkj.plug.excel.config.ExcelExport;
import com.cmkj.plug.excel.config.ExcelImport;
import lombok.Data;

import java.util.Date;

/**
 * @author wj
 * @date 2021/3/29
 * @describe 测试文件
 */
@Data
public class User {

    @ExcelExport(columnName = "ID")
//    @ExcelImport(columnIndex = 0)
    private Integer id;
    @ExcelExport(columnName = "名字")
    @ExcelImport(columnIndex = 1)
    private String name;
    @ExcelExport(columnName = "手机")
    @ExcelImport(columnIndex = 2)
    private String phone;
    @ExcelExport(columnName = "时间")
//    @ExcelImport(columnIndex = 3)
    private Date date;

}

2、测试文件

package com.cmkj.plug.excel.pojo;

import lombok.Data;

/**
 * @author wj
 * @date 2021/3/29
 * @describe 测试文件
 */
@Data
public class ExcelVo {

    /**
     * 文件路径
     */
    private String path;

    private String tempName;

    /**
     * 需要导入的起始行数 从第0行开始
     */
    private Integer startLine;

    /**
     * 导入最大行数
     */
    private Integer maxCount;

    private String depId;

    private String create;

}

3、Excel自定义导入注解

package com.cmkj.plug.excel.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author wj
 * @date 2021/3/29
 * @describe Excel自定义导入注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface ExcelImport {
	int columnIndex() default 0;

}

4、Excel自定义导出注解

package com.cmkj.plug.excel.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author wj
 * @date 2021/3/29
 * @describe Excel自定义导出注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface ExcelExport {
	String columnName() default "";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值