闲来无事,就研究下代码,只会照抄,也不明白其原理,东拼西凑,总算是可以用,纠结了好几次,看了好几次基础知识。
好了 不废话了,上代码
先创建注解类
package com.cinsos.cinsos.MyUtils.Excel;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME )
public @interface Excel {
/**
* 导出到Excel中的名字.
*/
public String name() default "";
/**
* 导出时在excel中每个列的高度 单位为字符
*/
public double height() default 14;
/**
* 导出时在excel中每个列的宽 单位为字符
*/
public double width() default 16;
}
再 创建注解字段所对应的实体类,也不确定没有什么什么用,就创建了一个
package com.cinsos.cinsos.MyUtils.Excel;
public class ExcelBean {
public String getName() {
return name;
}
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
@Override
public String toString() {
return "ExcelBean{" +
"name='" + name + '\'' +
", height=" + height +
", width=" + width +
'}';
}
public ExcelBean(String name, double height, double width) {
this.name = name;
this.height = height;
this.width = width;
}
public String name;
public double height;
public double width;
public void setName(String name) {
this.name = name;
}
public void setHeight(double height) {
this.height = height;
}
public void setWidth(double width) {
this.width = width;
}
}
再创建实体类来使用注解
package com.cinsos.cinsos.bean;
import com.cinsos.cinsos.MyUtils.Excel.Excel;
import java.util.Date;
public class User {
public String getUser_oid() {
return user_oid;
}
public String getUser_name() {
return user_name;
}
public String getUser_uname() {
return user_uname;
}
public String getUser_password() {
return user_password;
}
public Integer getUser_type() {
return user_type;
}
public String getUser_phone() {
return user_phone;
}
public String getUser_email() {
return user_email;
}
public Date getUser_greantime() {
return user_greantime;
}
public void setUser_oid(String user_oid) {
this.user_oid = user_oid;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public void setUser_uname(String user_uname) {
this.user_uname = user_uname;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
public void setUser_type(Integer user_type) {
this.user_type = user_type;
}
public void setUser_phone(String user_phone) {
this.user_phone = user_phone;
}
public void setUser_email(String user_email) {
this.user_email = user_email;
}
public void setUser_greantime(Date user_greantime) {
this.user_greantime = user_greantime;
}
@Override
public String toString() {
return "User{" +
"user_oid='" + user_oid + '\'' +
", user_name='" + user_name + '\'' +
", user_uname='" + user_uname + '\'' +
", user_password='" + user_password + '\'' +
", user_type=" + user_type +
", user_phone='" + user_phone + '\'' +
", user_email='" + user_email + '\'' +
", user_greantime=" + user_greantime +
'}';
}
@Excel(name = "用户唯一编号")
private String user_oid;
@Excel(name = "姓名")
private String user_name;
@Excel(name = "账号")
private String user_uname;
@Excel(name = "密码")
private String user_password;
@Excel(name = "类型")
private Integer user_type;
@Excel(name = "手机号码")
private String user_phone;
@Excel(name = "邮箱")
private String user_email;
@Excel(name = "创建时间")
private Date user_greantime;
}
再创建工具类来获取注解里的字段值
package com.cinsos.cinsos.MyUtils.Excel;
import com.cinsos.cinsos.bean.User;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtil {
/**
* 实体对象
*/
public Class clazz;
/**
* 将实体类通过对象传入
*
*/
public ExcelUtil(Class clazz)
{
this.clazz = clazz;
}
public List getValue() throws Exception {
//获取传入的类
Class c=this.clazz;
List list=new ArrayList();
Field[] fields =c.getDeclaredFields();
for(Field field:fields){
if(field.isAnnotationPresent(Excel.class)){
Excel D=field.getAnnotation(Excel.class);
System.out.println(D.name()+D.height()+D.width());
ExcelBean excelBean=new ExcelBean(D.name(),D.height(),D.width());
list.add(excelBean);
}
}
return list;
}
}
然后再调用工具类,获取注解内容
完事,自己写的,代码写的很随意,大家可以抄抄已测试 没有什么问题