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

被折叠的 条评论
为什么被折叠?



